[unity3d]通过C#脚本操作游戏对象

时间:2021-12-30 05:25:45

通过C#脚本创建游戏对象:

  • GameObject.CreatePrimitive(PrimitiveType type); 该方法可以创建一个原始游戏对象;其参数可调置为立方体,球体,圆柱体等系统默认提供的游戏对象;
  • AddComponent<typename T>(); 该方法用于给游戏对象添加一个组件;
  • GetComponent<typename T>(); 用于获取一个一个游戏对象的组件;
  • GetComponent<Renderer>().material.color;  该成员用于获取游戏对象Render的材质组件的颜色属性值;
  • transform.position; 设置游戏对象的位置; 其类型是一个Vector3的一个三维坐标结构;
代码实例如下:
if (GUILayout.Button ("创建立方体")) {
GameObject objCube = GameObject.CreatePrimitive(PrimitiveType.Cube); //创建一个Cube游戏对象
objCube.AddComponent<Rigidbody>(); //添加刚体游戏组件,使其获取物理重力特性,如重力等
objCube.name = "Cube1";
objCube.GetComponent<Renderer>().material.color = Color.cyan;
objCube.transform.position = new Vector3(0, 10, 0);
}

获取游戏对象:

  • 通过对象名获取对象:

GameObject.Find(string name); 实例代码如下:

using UnityEngine;
using System.Collections;

public class GUILayoutShow : MonoBehaviour {
Rect rectWnd1 = new Rect(25, 25, 200, 200);
bool isCubeRotate = false;
bool isSphereRotate = false;
string CubeInfo = "旋转立方体";
string SphereInfor = "旋转球体";
GameObject objCube = null;
GameObject objSphere = null;

// Use this for initialization
void Start () {
objCube = GameObject.Find ("Cube");
objSphere = GameObject.Find ("Sphere");
}

// Update is called once per frame
void Update () {
if (isCubeRotate) {
if(objCube != null){
objCube.transform.Rotate (0, Time.deltaTime * 200, 0);
}
}
if (isSphereRotate) {
if(objSphere != null){
objSphere.transform.Rotate (0, Time.deltaTime*200, 0);
}
}
}

void OnGUI(){

GUI.Box (new Rect (25, 25, 310, 310), "");

GUILayout.Width (150);
GUILayout.Height (50);
GUILayout.BeginArea (new Rect (35, 35, 200, 200));
GUILayout.BeginHorizontal ();
GUILayout.BeginVertical ();
if (GUILayout.Button ("创建立方体")) {
GameObject objCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
objCube.AddComponent<Rigidbody>();
objCube.name = "Cube1";
objCube.GetComponent<Renderer>().material.color = Color.cyan;
objCube.transform.position = new Vector3(0, 10, 0);
}
GUILayout.Space (5);
if (GUILayout.Button ("创建球体")) {
GameObject objSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
objSphere.AddComponent<Rigidbody>();
objSphere.name = "Sphere";
objSphere.GetComponent<Renderer>().material.color = Color.magenta;
objSphere.transform.position = new Vector3(0, 10, 0);
}
GUILayout.EndVertical ();

GUILayout.Space (5);
GUILayout.BeginVertical ();
if (GUILayout.Button (CubeInfo)) {
if(!isCubeRotate){
isCubeRotate = true;
CubeInfo = "停止旋转立方体";
}
else{
isCubeRotate = false;
CubeInfo = "旋转立方体";
}
}
GUILayout.Space (5);
if(GUILayout.Button (SphereInfor)){
if(!isSphereRotate){
isSphereRotate = true;
SphereInfor = "停止旋转球体";
}
else{
isSphereRotate = false;
SphereInfor = "旋转球体";
}
}
GUILayout.EndVertical ();

GUILayout.EndHorizontal ();
GUILayout.EndArea ();
}
}

  • 通过标签获取单个游戏对象:
先将一个游戏对象的Inspector窗口下,修改或选择Tag属性值,让游戏对象有一个单独唯一的标签名;通过方法Gameobject.FindWithTag()方法来获取游戏对象;例如: 将一个游戏对象Tag属性值改为MyTag,获取该游戏对象实例代码如下:
GameObject obj = GameObject.FindWithTap("MyTag");
</pre></blockquote></blockquote><ul><li>通过标签获取多个游戏对象:</li></ul><blockquote><blockquote><p>当有多个游戏对象,它们拥有相同的Tag属性时,也可以通过GameObject.FindObjeectsWithTag()方法,获取多个游戏对象;示例代码如下:<pre name="code" class="csharp">using UnityEngine;using System.Collections;public class FindObjectsWidhTag : MonoBehaviour {GameObject[] objMyTag;// Use this for initializationvoid Start () {objMyTag = GameObject.FindGameObjectsWithTag ("MyTag");}// Update is called once per framevoid Update () {foreach (GameObject obj in objMyTag) {Debug.Log ("以"+obj.tag+"标签为游戏对象的名称 "+obj.name);}}}