您的位置:首页 > 移动开发 > Unity3D

Unity获取物体组件调用

2015-03-09 23:00 148 查看
本文只记录了调用物体的脚本,Unity界面相关操作并未介绍。

需要实现的内容如下:

1. 场景 一个光源, 一个cube1 一个cube2

一个sphere 一个Cylinder

点击cube1 可以控制 光源亮度 cube1变大变小 cube2旋转 sphere颜色 cylander走停

using UnityEngine;
using System.Collections;

public class hw0309a : MonoBehaviour {

// Use this for initialization
public bool t=false;
public Vector3 V3;
void Start () {
V3 = new Vector3 (2, 2, 2);
Debug.Log (transform.localScale);

}

// Update is called once per frame
void 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:本文将控制物体的脚本放在一起,实际上,不同功能可以创建不同脚本,这样,脚本可以重复利用,创建新的物体时,可以根据需要绑定不同的脚本,十分方便。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: