Unity 视频总结(一) 初级编程

初级编程 – Unity Learn

  • 输入 Input.GetKeyDown(KeyCode.R)
  • 获取在组件并设置色彩 GetComponent().material.color = Color.green;
  • update()函数是更新帧
using Unity巩立姣Engine;
using System.Collections;
public class ExamplgoogleeBehaviourScript : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
GetComponent<Renderer> ().material.color = Color.red;
}
if (Input.Get变量KeyDown(KeyCode.G))
{
Get变量英文Component&l变量的界说t;Renderer>().material.color =枸杞 Col宫颈癌or.green;
}
if (Input.GetKeyDown(KeyCode.B))
{
GetComponent<Rendere变量的界说r>().material.color = Color.blue;
}
}
}

  • start函数是初步函数
  • awake函数也是初步函数
using UnityEngine;
using System.Collections;
public clas变量英文s VariablesAndFunctions : MonoBehaviou宫颈癌r
{
int myInt = 5;
void Start ()
{
myInt = MultiplyByTwo(变量类型有哪些myInt);
Debug枸杞.Log (myInt);
}
int MultiplyByTwo (int number)
{
int变量之间的联系 ret;
ret = number * 2;
return ret;
}
}

  • 这个时分的transform值自身
using UnityEngine;
using System.Collections;
public狗狗币 class BasicSyntax : MonoBehaviour
{
vGooid Start ()
{
Debug变量与函数.Log(transform.position.x);
if(t巩立姣ransform.position.y <= 5f)
{
Debug.Log ("I'm about to hit the ground!");
}
}
}

  • Time.deltaTime 差距时间
using Un变量与函数ityEngine;
using S变量min表明什么类型的变量ystem.Collections;
public class IfStatements : MonoBehaviour
{
f宫颈癌loat coffeeTemperGoature = 85.0f;
float hotLimitTemperature = 70.0f;
float coldLimitTemperature = 40.0f变量之间的联系;
void Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
Tem变量泵peratureTe巩立姣st();
coffeeTemperat宫颈癌ure -枸杞= Time.deltaTime * 5f;
}
void TemperatureTest ()
{
// 假定咖啡的温度高于最热的饮用温度...
if(c变量的界说offeeTemperature > hotLimitTemperature)
{
// ... 实施此操作。
print("Coffee is too hot.");
}
// 假定不是,但咖啡的温度低于最冷的饮用温度...
else if(c枸杞offeeTemperature < coldLimitTemperature)
{
// ... 实施此操作。
print("Coffee is too cold.");
}
// 假定两者都不是,则...
else
{
// ... 实施此操作。
print("Coffee is just right.");
}
}
}

  • awake 即使没有变量类型有哪些启用脚本也能运转,首要是初始化引用
  • star变量提高t 初次更新置前调用,一定需求启用脚本
using Unity变量min表明什么类型的变量Engine;
using Syste变量名的命名规矩m.Collections;
public class AwakeAndStart : MonoBehaviour
{
void Awake ()
{龚俊
Debug.Log("Awake called."宫颈癌);
}
void Start ()
{
Debu变量是什么意思g.Log("Start called."巩立姣);
}
}

  • FixedU变量是什么意思pdate固定距离调用
  • Updat工商银行客服电话e 一帧跑完再调用
using UnityEngine;
using System.Collections;
public class UpdateAndFixedUpdate : MonoBehaviour
{
voi宫颈癌前期症状d FixedUpdategoogle ()
{
Debug.Log("FixedUpdate time :" + Time.deltaTime);
}
void Upda变量min表明什么类型的变量te ()
{
Debug.Log("Update time :" + Time.deltaTime);
}
}

  • 启用/禁用组件
usin工商银行客服电话g UnityEngine;
using System.Collections;
public class EnableComponents : MonoBehaviour
{
pri工商银行vate Light myLight;
void Start ()
{
myLight = GetComponent<L工商银行ight>();
}
void变量是什么意思 Update ()
{
if(Input.GetKeyUp(KeyCod变量之间的联系e.Spa龚俊ce))公积金
{
myLight.enabled = !myLight.enabled;
}
}
}

  • gameObj狗狗币ect 也是tran变量的界说sform自变量身吗
using UnityEngine;
using System.Collections;
public class ActiveObjects : Mo变量的界说noBehaviour
{
void Start ()
{
gameObject.SetActive(false);
}
}
using UnityEngine;
using狗狗币 S巩立姣ystem.Collections;
public class CheckState : M巩立姣onoBehaviour
{
public GameObject myObject;
void Start ()
{
Debug.Log("Active Self: " + myObject.变量名的命名规矩activeSelf);
Debug.Log("Active in Hierarchy" + myObject.activeInHierarchy);
}
}

  • Input.GetKey(KeyCode.UpArrow) 这个不必GetKeyDown
  • Vector3.forward 由所以局部坐标,所以可以这么弄
using Uni变量英文tyEngine;
using System.Collections;
public class TransformFunction变量是什么意思s : MonoBehaviour
{
public float moveSpeed = 10f;
public float turnSpeed = 50f;
void Update ()
{
if(Input.GetKey(KeyCod变量min表明什么类型的变量e.UpArrow))
transf变量类型有哪些orm.Translat工商银行客服电话e(Vector3.forward * move宫颈癌Speed * T公积金ime.deltaTime);
if(Input.GetKey(KeyCode.Down龚俊Arrow))
transform.Translate(-Vector3.forward * moveSp变量类型有哪些eed * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftArrow))
transform.Rotate(Vector宫颈癌3.up,枸杞 -turnSpeed * Time.deltaTime);
if(Input.GetKey(KeyCod变量之间的联系e.RightArrow))
transform.Rotate(Vector3.up, turnSpeed *变量的界说 Time.deltaTime);
}
}

  • 这个脚本首要用宫颈癌前期症状在相机上
u变量名的命名规矩sing UnityEngooglegine;
using S变量名的命名规矩ystem.Collections;
public class CameraLookAt : Mo变量泵noBehaviour
{
public Transform target;
void Update ()
{
transform.LookAt(target);
}
}

using UnityEngine变量英文;
using System.Collections;
public class DestroyBasic : M宫颈癌onoBehaviour
{
void Update ()
{
if(Input.GetKey(KeyCode.Sp变量min表明什么类型的变量ace))
{
Destroy(gameObject);
}Go
}
}
using UnityEngin变量泵e;
using System变量之间的联系.Collectio变量的界说ns;
public class DestroyOther : MonoBehaviour
{
public GameObject other;变量
void Up枸杞date ()
{
if(Input.GetKey(KeyCode.S工商银行客服电话pace))
{
Destroy(other);
}
}
}
using UnityEngine;
using System.Collect变量的界说ions;
public class DestroyComponent : MonoBehaviour
{
void Update ()
{
if(Input.GetKey(KeyCode.Space))
{
Destroy(GetComponent<MeshRenderer>());
}
}变量min表明什么类型的变量
}

  • GetKeyDown 按下去的那个进程,不包括继续按的进程
  • GetKey 按下去的那个进宫颈癌前期症状程+继续按的进程
  • GetKeyUp 松开的进程
using UnityEngine;
using System.Collect狗狗币ions;
public class KeyInput : MonoBehaviour
{
public GUITexture graphic;
public Texture2D standard;
public Texture2D downgGofx;
public Texture2D upgfx;
public Texture2D heldgfx;
void Start()
{
graphic.texture = standard;
}
void Update ()
{
bool down = Input.GetKe变量yDown(KeyCode.Space);
bool held = Input.GetKey(KeyCode.Space)变量之间的联系;
bool up = Input.GetKe工商银行客服电话yUp(KeyCode.Sp变量名的命名规矩ace);
if(down)
{
graphic.texture = downgfx;
}
else if(held)
{
graphic.tex公积金ture = heldgfx;
}
else if(up)
{
graphic.texture = upgfx;
}
else
{
graphic.texture = sta变量的界说ndard;
}
gui工商银行Text.text = " " + down + "n " + held + "n " + up;
}
}
  • jump宫颈癌的来历

Unity 视频总结(一) 初级编程

using UnityEngine;
using S变量英文ystem.Collections;
public class ButtonInput : MonoBehaviour
{
public GUITexture graphic;
public Texture2D standagooglerd;
public Te宫颈癌前期症状xture2D downgfx;
public Texture2D upgfx;
public Texture2D heldgfx狗狗币;
void Start()
{
grapGohic.texture = standard;
}
void Update ()
{
bool down = Input.GetButtonDown("Jump");
bool held = Input.GetButton("Jump");
bool up = Input.GetButtonUp("Jump");
if(变量的界说down)
{
graphi变量类型有哪些c.texture = downgfx;
}
else if(held)
{
graphic.texture = heldgfx;
}
else if(up)
{
graphic.texture = upgfx;
}
else
{
graphic.tex龚俊tur变量提高e = stan变量提高dard;
}
guiText.text = " " + down + "n " + held + "n " + up;
}
}

using UnityEngine;
using System.Collections;
public class AxisExample : MonoBehaviour
{
public float range;
public GUIText textOutput;
void Update ()
{
float h = Input.GetAxis("Horizontal");
float xPos = h * range;
transform.position = new Vector3(xPos变量, 2f, 0);
textOutput.text = "Value Returned: "+h.ToString("F2")巩立姣;
}
}
using UnityEngi变量min表明什么类型的变量ne;
using System.Collections;
public class AxisRawExample : MonoBehav工商银行iour
{
public float range;
public GUI变量提高Te变量与函数xt textOu变量与函数tput;
void Update ()
{
float h = Input.GetAxisRaw("Horizontal");
float xPos = h * range;
trans变量min表明什么类型的变量forgooglem.position = new Vector3(xPos, 2f, 0);
te工商银行xtOutputgoogle.text = "Value狗狗币 Returned: "+h.ToString("F2");
}
}

  • 鼠标事情
using UnityE公积金ngine;
using System.Collec工商银行tions;
public class MouseClick : MonoBehaviour
{
void OnMouseDown ()
{
rigidbody.AddForce(-transform.forward * 500f);
rigidbody.useGravity = true;
}
}

  • 获取其他脚本的变量
  • 少用Get变量的界说Component 会卡
using UnityEngine;
using System.Collections;
public class UsingOtherCoGomponents : MonoBehaviour
{
public GameObject otherGameObject;
private AnotherScript anotherScript;
pri狗狗币vate YetAnotherScript yetAnotherScript;
private BoxCollider枸杞 box变量提高Col;
void Awake ()
{
anotherScript = GetComponent<AnotherScript>();
yGoetAnotherScript = othe公积金rGameObject.GetComponent<YetAnotherScript>();
bo工商银行xCol = otherGameObject.GetCom变量类型有哪些ponent<BoxCollider>();龚俊
}
void Start ()
{
boxCol.Gosize = new Vector3(3,3,3);
Debug.Log("The player's score is " + anotherScrip宫颈癌前期症状t.playerScore);
Debug.Log("The player has died " + yetAno变量英文therScript.numberOfPlayerDeaths + " times");
}
}
using UnityEngine;
using System.Collections;
public class AnotherScri枸杞pt : MonoBehaviour
{
public int playerScore = 9001;
}
using UnityEn龚俊gine;
using System.Collections;
public class YetAn工商银行otherScript : MonoB变量ehaviour
{
pub变量是什么意思lic int numberOfPlayerDeaths = 3;
}

  • Instantiate是用来树立一个perfab的,但输入参数可所以Rigidbody
  • barrelEnd是用来闪现方位的
  • a变量提高ddForce是运用某个方向的力
using UnityEngine;
using Syst变量提高em枸杞.Collections;
pub巩立姣lic class UsingInstantiate : MonoBehaviour
{
public Rigidbody roGocketPr宫颈癌前期症状efab;
public Transform barrelEnd;
void Update (宫颈癌前期症状)
{
if变量min表明什么类型的变量(Input.GetButtonDown("Fire1"))
{
Rigidbody rocketInstance;
rocketInstance = Instantiate(rocketP工商银行refab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
rocketInstance.AddForce(barrelEnd.forward * 5000)龚俊;
}
}
}
  • 过1.5s后删去方针
using UnityEngine;
using System.Collection变量类型有哪些s;
public class RocketDestruction : MonoBehGoaviour
{
void Star变量与函数t()
{
Destroy (gameObject, 1.5f);公积金
}
}

  • GameObject好像是一切方针
  • Tag是在这里增加

Unity 视频总结(一) 初级编程

using UnityEngine;
using System.Collectio变量的界说ns;
public class Arrays : MonoBehaviour
{
public GameObject[] players;
void Start ()
{
players = GameObject.FindGameObjectsWithTag("Player");
for(int i = 0; i < players.Length; i++)
{
Debug.Log("Player Number "+i+" is named "+players[i].name);
}
}
}变量min表明什么类型的变量

using UnityEngine工商银行客服电话;
using System.Collections;
public class InvokeScript : MonoBehaviour
{
public GameObject target;
void Start()
{
Invoke ("SpawnObject", 2);
}
void SpawnObject()
{变量名的命名规矩
Instantia狗狗币te(target, new Vector3(0, 2, 0), Quaternion.iden变量之间的联系tity);
}
}
using UnityEngine;
using System.Collections;
public class InvokeRepeating : MonoBehaviour
{
public GameObject target变量英文;
void Start()
{
Invoke公积金Repeati变量泵ng("Spaw枸杞nObj变量之间的联系ect", 2, 1);
}
void SpawnObject()
{
float x = Random.Range(-2.0f, 2.0f);
float z公积金 = Random.Range(-2.0f, 2.0f);
Instantiate(target, new Vector3(x, 2, z), Quaternion.identity);
}
}

  • Invoke与I变量类型有哪些nvokeRepe变量与函数ating 异步触发函数
  • 第一个参数都是函数,第二个参数是时间
us枸杞ing UnityEngine;
using System.Collection变量与函数s;
public class InvokeScript : MonoBehaviour
{
public变量类型有哪些 GameObject target;
void Start()
{
In工商银行客服电话voke ("SpawnObject", 2);
}
vo狗狗币id SpawnObject()
{
Instantiate(targ龚俊et, new Vector3(0, 2, 0), Quaternion.identity);
}
}
using UnityEngi宫颈癌ne;
using System.Collections;
public class InvokeRepeating : MonoBehaviour
{
public GameObject target变量的界说;
void Start()
{
InvokeRepeating("SpawnObject", 2, 1);
}
void SpawnObject()
{
float x = Random.Range(-2.0f, 2.0f);
float z = Random.Range(-2.0f, 2.0f);
Instantiate(target, new Vector3(x, 2, z), Quaternion.identity);
}
}

  • 枚举
using UnityEngine;
using System.Collections;
public变量英文 class EnumScript : MonoBehaviour
{
enum Direction {North, East, South, West};
void Start ()
{
Direction myDirection;
myDirection = Direction.North;
}
Direction ReverseDirection (Direction dir)
{
if(di工商银行客服电话r == Direction.North)
dir = Direction.South;
else if(dir == Direction.South)
dir = Direc变量之间的联系tion.North;
else if(di枸杞r == Direction.East)
dir = Direction.West;
else if(dir == Direction.West工商银行客服电话)
dir = Direction.East;
return di变量的界说r;变量的界说
}
}

发表评论

提供最优质的资源集合

立即查看 了解详情