Unity 3D改变旋转角度
float value_x=0;
float value_y=0;
float value_z=0;
GameObject cube ;
GameObject cylinder;
// Use this for initialization
void Start () {
cube = GameObject.Find ("Cube");
cylinder = GameObject.Find ("Cylinder");
}
void OnGUI()
{
GUILayout.Box ("改动X轴角度");
value_x = GUILayout.HorizontalSlider (value_x,-180,180,GUILayout.Width (200));
GUILayout.Box ("改动Y轴角度");
value_y = GUILayout.HorizontalSlider (value_y,-180,180,GUILayout.Width (200));
GUILayout.Box ("改动Z轴角度");
value_z = GUILayout.HorizontalSlider (value_z,-180,180,GUILayout.Width (200));
cube.transform.rotation =Quaternion.Euler ( value_x,value_y,value_z);
GUILayout.Box ("角度为"+cube.transform.rotation);
}
// Update is called once per frame
void Update () {
}
Unity 3D工具栏选项菜单
//记录选择了哪个按钮的ID
int select;
string[] barsources;
bool selectToggle1;
bool selectToggle2;
void OnGUI()
{
int oldSelect = select;
select = GUI.Toolbar(new Rect(10,10,barsources.Length*100,30),select,barsources);
if(select != oldSelect)
{
selectToggle1 = false;
selectToggle2 = false;
}
switch(select)
{
case 0:
selectToggle1 = GUI.Toggle(new Rect(10,50,200,30),selectToggle1,"工具栏1的单项选择按钮-1");
selectToggle2 = GUI.Toggle(new Rect(10, 80, 200, 30), selectToggle2, "工具栏1的单项选择按钮-2");
break;
case 1:
selectToggle1 = GUI.Toggle(new Rect(10, 50, 200, 30), selectToggle1, "工具栏2的单项选择按钮-1");
selectToggle2 = GUI.Toggle(new Rect(10, 80, 200, 30), selectToggle2, "工具栏2的单项选择按钮-2");
break;
case 2:
selectToggle1 = GUI.Toggle(new Rect(10, 50, 200, 30), selectToggle1, "工具栏3的单项选择按钮-1");
selectToggle2 = GUI.Toggle(new Rect(10, 80, 200, 30), selectToggle2, "工具栏3的单项选择按钮-2");
break;
case 3:
selectToggle1 = GUI.Toggle(new Rect(10, 50, 200, 30), selectToggle1, "工具栏4的单项选择按钮-1");
selectToggle2 = GUI.Toggle(new Rect(10, 80, 200, 30), selectToggle2, "工具栏4的单项选择按钮-2");
break;
}
}
// Use this for initialization
void Start()
{
select = 0;
barsources = new string[] { "第一个工具栏", "第二个工具栏", "第三个工具栏", "第四个工具栏" };
selectToggle1 = false;
selectToggle2 = false;
}
// Update is called once per frame
void Update()
{
}
Unity 3D 公转Around 的用法
void Update () {
// transform.Rotate(Vector3.up * Time.deltaTime * -Gongzhuang);
/// transform.RotateAround(new Vector3(0, 0, 4.031555f), Vector3.up, 20 * Time.deltaTime);
///
transform.RotateAround(new Vector3(0, 0, 0), Vector3.up, 20 * Time.deltaTime);//三个参数,旋转中心坐标 旋转速度
}
int Gongzhuang = 360;
void OnGUI()
{
if (GUI.RepeatButton(new Rect(10, 40, 150, 30), "月球公转"))
{
//
}
}
Unity 3D滚动条
Vector2 scrollviewposition;
// Use this for initialization
void Start () {
scrollviewposition[0] = 0;
scrollviewposition[1] = 0;
}
void OnGUI()
{
scrollviewposition = GUI.BeginScrollView(new Rect(0, 0, 300, 300), scrollviewposition, new Rect(0, 0, 300,250), true, false);
GUI.Label(new Rect(10, 10,50,50), "测");
GUI.EndScrollView();
}
// Update is called once per frame
void Update () {
}
Unity 3D 接受任务绘制窗口实例
// Use this for initialization
void Start()
{
}
// int sudu = 20;
// private Transform transform;
// Update is called once per frame
bool isWin = false;//添加一个标示,表示窗口的状态
string Mee = "亲爱的勇士们";
string Mess = "愚蠢的人类呀!";
string M = "我可以实现你的愿望!";
string t1 = "实现";
string t2 = "放弃";
int click = 0;//按钮的点击次数
string[] Huoba;
void OnGUI()
{
GUI.Label(new Rect(20, Screen.height/2, 200, 250), Mee+click);//左边的说明文字
if (isWin)
{
GUI.Window(0, new Rect(Screen.width / 2, Screen.height / 4, 300, 100), Mouse,Mess );
if (GUI.Button(new Rect(Screen.width / 2 + 25, Screen.height / 4 + 60, 50, 30),t1))
{
click++;//每点击一次按钮,click++
// isWin = false;
Mee = "帮我搜集神器,我帮你完场梦想";
Mess = "寻找十方神器!";
M = "如果你找到了十方神器,我给你荣华富贵!";
t1 = "开始寻找";
t2 = "放弃寻找,回家睡觉";
if (click == 2)//如果点击次数是2的话,就关闭窗口
{
isWin = false;
// GetComponent<Light>().enabled = false;//关闭组件
// gameObject.SetActive(false);//设置活动
// GetComponent<Light>().intensity = 0.0f;//减弱灯光强度
// GetComponent<Light>().range = 0.0f;//点光源范围
}
}
if (GUI.Button(new Rect(Screen.width / 2 + 220, Screen.height / 4 + 60, 50, 30), t2))//点击取消按钮
{
isWin = false;
Mee = "呵呵哒!";
}
}
}
void Mouse(int Winid)
{
GUI.Label(new Rect(80, 30, 150, 50), M);//窗体的标签文字
}
void Update()
{
//if (click == 0)
//{
// isWin = false;
//if (Input.GetMouseButtonDown(1))
//{
// RaycastHit hit;
// Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if (Physics.Raycast(ray, out hit, 100))
// {
// Debug.Log(hit.collider.gameObject.name);
// }
//}
//}
// play = GameObject.Find("Culb");
//0表示鼠标左键1 表示鼠标右键
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100))
{
if (hit.collider.gameObject.name == "she")//如果游戏对象是蛇
{
isWin = true;
if (click == 2)//满足点击次数
{
isWin = false;
if (transform.Find("Point light").GetComponent<Light>().tag == "GameBoss")
{
transform.Find("Point light").GetComponent<Light>().range = 0.0f;
}
//transform.Find("Point light").GetComponent<Light>().enabled = false;
}
}
}
}
if (click == 2)
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100))
{
//Debug.Log(hit.collider.gameObject.name);
if (hit.collider.gameObject.name == "Cubeone" || hit.collider.gameObject.name == "Cubeone02")//拾取物品
{
Destroy(hit.collider.gameObject);
}
}
}
}
}
Unity 3D判断游戏执行的顺序
// Use this for initialization
void Start () {
print ("开始执行StartCoroutine,, "+Time.time);
StartCoroutine ("aaa");
print ("结束执行StartCoroutine,, "+Time.time);
}
IEnumerator aaa()
{
print ("开始执行aaa,, "+Time.time);
yield return new WaitForSeconds (3);
//
print ("结束执行aaa,, "+Time.time);
}
// Update is called once per frame
void Update () {
}
Unity 3D前后左右移动
// Use this for initialization
void Start()
{
all = Resources.LoadAll("knight");
nowFrame = 0;
countFrame = all.Length;
fps = 10;
}
int x = 100;
int y = 100;
object[] all;
void OnGUI()
{
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), (Texture)Resources.Load("back"), ScaleMode.StretchToFill, false, 0);
DrawAnimatiion(new Rect(x, y, 64, 64));
if (GUILayout.RepeatButton("向前"))
{
x += 2;
}
if (GUILayout.RepeatButton("向后"))
{
x -= 2;
}
if (GUILayout.RepeatButton("向下"))
{
y += 2;
}
if (GUILayout.RepeatButton("向上"))
{
y -= 2;
}
}
void FixedUpdate()
{
}
int nowFrame = 0;
int countFrame = 0;
float time;
int fps = 0;
void DrawAnimatiion(Rect r)
{
GUI.DrawTexture(r, (Texture)all[nowFrame], ScaleMode.StretchToFill, true, 0);
time += Time.deltaTime;
if(time >= 1.0/fps)
{
nowFrame++;
time = 0;
}
if(nowFrame >=countFrame)
{
nowFrame = 0;
}
}
// Update is called once per frame
void Update()
{
}
Unity 3D 添加脚本移除脚本 添加背景颜色
public Texture myTexture;
GameObject myObj;
Renderer render;
// Use this for initialization
void Start () {
myObj = GameObject.Find ("Cube");
myObj.AddComponent (typeof(UnityScript03));//添加脚本
render = (Renderer)myObj.GetComponent <Renderer>();
}
int a=10;
int b=10;
int c=10;
void OnGUI()
{
if (GUILayout.Button ("添加颜色")) {
myObj.GetComponent<Renderer>().material.color = Color.red;
}
if (GUILayout.Button ("移除颜色")) {
a++;
b++;
c++;
myObj.transform.position = new Vector3(10,10,10);
myObj.transform.localScale = new Vector3(a,b,c);
}
if (GUILayout.Button ("添加背景图")) {
render.material.mainTexture=myTexture;
}
if (GUILayout.Button ("移除脚本")) {
Destroy(myObj.GetComponent<UnityScript03>());
}
}
// Update is called once per frame
void Update () {
}
Unity 3D 添加移除窗口
// Use this for initialization
void Start () {
windowList.Add(new Rect(windowList.Count*200,100,200,150));
}
ArrayList windowList = new ArrayList();
void OnGUI()
{
int count = windowList.Count;
for (int i = 0; i < count;i++ )
{
windowList[i] = GUILayout.Window(i, (Rect)windowList[i],myWindow,"这是第"+i+"个窗口");
}
}
void myWindow(int winid)
{
if (GUILayout.Button("添加一个窗口"))
{
windowList.Add(new Rect(windowList.Count * 200, 100, 200, 150));
}
if (GUILayout.Button("移除该窗口"))
{
windowList.Remove(windowList[winid]);
//windowList.RemoveAt(winid);
}
GUI.DragWindow();
}
// Update is called once per frame
void Update () {
}
Unity 3D 添加组件 控制组件旋转
string AddCub = "添加正方体";
string AddSphere = "添加球体";
bool isRotateCub = false;
bool isRotatreSphere = false;
string CubRotate="开始选装方体";
string SphereRotate = "开始旋转球体";
public int Angle=50;
GameObject Cub;
GameObject Sphere;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Cub = GameObject.Find ("Cub1");
Sphere = GameObject.Find ("Sphere1");
Diaoluo (isRotateCub, Cub);
Diaoluo (isRotatreSphere, Sphere);
}
void Diaoluo(bool Rotate,GameObject obj)
{
if (obj) {
if(Rotate)
{
obj.transform.Rotate(Vector3.up*Time.deltaTime*Angle);
}
}
}
void OnGUI()
{
if(GUILayout.Button(AddCub))
{
GameObject objCub = GameObject.CreatePrimitive(PrimitiveType.Cube);
objCub.name="Cub1";
objCub.AddComponent(typeof(Rigidbody));//添加刚体
//objCub.rigidbody.useGravity = false;//移除刚体
objCub.GetComponent<Renderer>().material.color = Color.red;
objCub.transform.position = new Vector3(0,10,0);
}
if(GUILayout.Button(AddSphere))
{
GameObject objSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
objSphere.name="Sphere1";
objSphere.AddComponent(typeof(Rigidbody));
objSphere.GetComponent<Renderer>().material.color = Color.green;
objSphere.transform.position = new Vector3(0,10,0);
}
if (GUILayout.Button (CubRotate)) {
if(!isRotateCub)
{
isRotateCub = true;
CubRotate="停止旋转方体";
}else
{
isRotateCub = false;
CubRotate="开始旋转方体";
}
}
if (GUILayout.Button (SphereRotate)) {
if(!isRotatreSphere)
{
isRotatreSphere = true;
SphereRotate="停止旋转球体";
}else
{
isRotatreSphere = false;
SphereRotate="开始旋转球体";
}
}
}