標籤:style blog http color os 2014
指令碼語言:C#
1、在Unity工程中建立兩個物體:Cube和Sphere
2、分別為Cube和Sphere添加指令碼CubeScript和SphereScript:
在SphereScript這兩個定義一個函數DoSomething(),指令碼具體代碼如下:
using UnityEngine;using System.Collections;public class SphereScript : MonoBehaviour { public int radius = 5; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void DoSomething(){ Debug.Log("Hello ~"); }}
3、在CubeScript指令碼中實現對物體Sphere的訪問,使它的顏色變為紅色,和指令碼SphereScript中DoSomething函數的訪問,具體代碼如下:
using UnityEngine;using System.Collections;public class CubeScript : MonoBehaviour { // Use this for initialization void Start () { SphereScript a = null; // 擷取SphereScript指令碼 a = GameObject.Find("Sphere").GetComponent<SphereScript>(); Debug.Log ("SphereScript‘s a = " + a.radius); Debug.Log("SphereScript‘s function Domething :"); //調用SphereScript指令碼的函數 a.DoSomething (); } // Update is called once per frame void Update () { GameObject target = null; //訪問Sphere引用物體 target = GameObject.Find ("Sphere"); //使球體顏色變為紅色 target.renderer.material.color = Color.red; }}
4、運行效果: