Unity擷取物體組件調用

來源:互聯網
上載者:User

本文只記錄了調用物體的指令碼,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:本文將控制物體的指令碼放在一起,實際上,不同功能可以建立不同指令碼,這樣,指令碼可以重複利用,建立新的物體時,可以根據需要綁定不同的指令碼,十分方便。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.