本文只記錄了調用物體的指令碼,Unity介面相關操作並未介紹。
需要實現的內容如下:
1. 情境 一個光源, 一個cube1 一個cube2
一個sphere 一個Cylinder
點擊cube1 可以控制 光源亮度 cube1變大變小 cube2旋轉 sphere顏色 cylander走停
using UnityEngine;using System.Collections;public class hw0309a : MonoBehaviour {// Use this for initializationpublic bool t=false;public Vector3 V3;void Start () {V3 = new Vector3 (2, 2, 2);Debug.Log (transform.localScale);}// Update is called once per framevoid Update () {//6.cylinder走停if (t == true) {V3.x += 0.1f;Debug.Log (V3);}}//void OnMouseDown(){//2.cube1變大變小t = !t;Debug.Log (t);if (t == true) {transform.localScale +=new Vector3 (1.0F, 1.0F, 1.0F);Debug.Log (transform.localScale);} else{transform.localScale -=new Vector3 (1.0F, 1.0F, 1.0F);}}}
using UnityEngine;using System.Collections;public class hw0309b : MonoBehaviour {public hw0309a h1;public GameObject cylinder;public GameObject sphere;public GameObject cube2;public GameObject Directional_light;public Material Ms1;public Material Ms2;public bool t1;void Start () {//sphere.GetComponent<MeshRenderer> ().material = Ms1;}//隨時間改變光照強度void Update () {//5.cylinder走停cylinder.transform.position = h1.V3;//4.sphere顏色t1 = h1.t;if (t1 == false){sphere.GetComponent<MeshRenderer> ().material = Ms1;if(0.64f<=Directional_light.light.intensity){Directional_light.light.intensity-=0.01f;} } else {sphere.GetComponent<MeshRenderer> ().material = Ms2;//3.cube2旋轉cube2.transform.Rotate(Vector3.right * 5);cube2.transform.Rotate(Vector3.up * 5, Space.World);//1.可以控制 光源亮度if(Directional_light.light.intensity<=8.0f){Directional_light.light.intensity+=0.01f;}}}}
ps:本文將控制物體的指令碼放在一起,實際上,不同功能可以建立不同指令碼,這樣,指令碼可以重複利用,建立新的物體時,可以根據需要綁定不同的指令碼,十分方便。