using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
// C# coroutine // C# 协同程序
IEnumerator SomeCoroutine () {
// Wait for one frame // 等一帧
yield return 0;
// Wait for two seconds // 等两秒
yield return new WaitForSeconds (2);
}
}
对物体的移动以及旋转通过Transfrom操作。
public Transform transform;
void Update()
{
//旋转操作
transform.Rotate(0,5,0);
//移动操作
transform.Translate(0,0,2);
}
public Vector3 aposition = new Vector3(1,1,1);
public Vector3 a = new Vector3(1,1,1);
public Vector3 b = new Vector3(1,0,1);
float result = Vector3.Dot(a,b);
float result = Vector3.Cross(a,b);
float angle = Vector3.Angle(a,b);
float dis = Vector3.Distance(a,b);
float dis = Vector3.Distance(a.normalized,b.normalized);
用static关键字创建全局变量,这是创建了一个名为someGlobal的全局变量.
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public static int someGlobal = 5;
public void Awake() {
print(typeof(someGlobal));
typeof(someGlobal) = 1;
}
}
从另一个脚本访问它,你需要用”脚本名加一个小圆点再加上全局变量名”.
print(TheScriptName.someGlobal);
TheScriptName.someGlobal = 10;
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject obj;
void Start() {
obj =GameObject.Find("Cube");
}
void Update()
{
if(obj != null)
{
obj.transform.Rotate(0, Time.deltaTime * 200, 0);
}
}
}
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject[] obj;
void Start() {
Myobjs = GameObject.FindGameObjectsWithTag("Cube");
}
void Update()
{
if(Myobjs != null)
{
foreach(GameObject obj in Myobjs)
{
Debug.log("以"+obj.tag+"标签为游戏对象的名称"+obj.name);
}
}
}
public Transform prefab;
void Start()
{
int i = 0;
while(i < 10)
{
Instantiate(prefab,new Vector3(i*2.0f,0,0),Quaternion.identity);
i++;
}
}
public Rigidbody obj;
void Update()
{
Rigidbody clone;
clone = Instantiate(obj,transform.position,transform.rotation);
}