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

Unity 3D 第三更

2015-09-08 14:20 615 查看
Unity 围绕某点进行旋转

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()
{

if(GUILayout.RepeatButton ("立方体绕着圆柱体旋转"))
{
cube.transform.RotateAround (cylinder.transform.position,cylinder.transform.up,Time.time*0.5f);

}

}
// Update is called once per frame
void Update () {

}


Unity 3D文本框

string name="";
string pass="";
string mes;
void OnGUI()
{
GUI.Label(new Rect(10,10,Screen.width,Screen.height),mes);

GUI.Label(new Rect(10,30,50,30),"账号:");
GUI.Label(new Rect(10,60,50,30), "密码:");
name= GUI.TextField(new Rect(60,30,150,30),name,15);
pass = GUI.PasswordField(new Rect(60,60,150, 30), pass,"*"[0]);
if(GUI.Button(new Rect(10,90,50,30),"提交"))
{
mes = string.Format("注册的名称为:{0},密码为:{1}",name,pass);
}
}

// Use this for initialization
void Start () {
mes = "请您输入用户名和密码";
}

// Update is called once per frame
void Update () {

}


Unity 3D 物体移动选装

void Awake()
{
}
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
int translateSpeed = 20;
int rotateSpeed = 100;
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 150, 30), "逆时针旋转"))
{
transform.Rotate(Vector3.up * Time.deltaTime * -rotateSpeed);
}
if (GUI.Button(new Rect(10, 40, 150, 30), "顺时针旋转"))
{
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
}

if (GUI.Button(new Rect(10, 70, 150, 30), "向左移动"))
{
transform.Translate(Vector3.left * Time.deltaTime * translateSpeed);
}
if (GUI.Button(new Rect(10, 100, 150, 30), "向前移动"))
{
transform.Translate(Vector3.forward * Time.deltaTime * translateSpeed);
}
if (GUI.RepeatButton(new Rect(10, 130, 150, 30), "不停地逆时针旋转"))
{
transform.Rotate(Vector3.up * Time.deltaTime * -rotateSpeed);
}
if (GUI.RepeatButton(new Rect(10, 160, 150, 30), "不停地顺时针旋转"))
{
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
}
}


Unity 3D线性布局

void OnGUI()
{
GUILayout.BeginHorizontal();
GUILayout.Label("asdasdsa");

GUILayout.Button("asd");

GUILayout.BeginVertical();
GUILayout.Label("123123123");
GUILayout.Box("实打实大萨达的萨达");
GUILayout.EndVertical();

GUILayout.EndHorizontal();
}
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}


Unity 3D音量调节

int volumnValue;
float percentValue;
// Use this for initialization
void Start () {
volumnValue = 100;
percentValue = 0.0f;
}

void OnGUI()
{
volumnValue = (int)GUI.HorizontalSlider(new Rect(10, 10, 150, 30),volumnValue,0,100);

percentValue = (int)GUI.VerticalSlider(new Rect(10, 50, 30, 150),percentValue,0, 100);

GUI.Label(new Rect(165,10,100,30),"声音:"+volumnValue+"%");
GUI.Label(new Rect(10, 205, 100, 30), "百分比:" + percentValue + "%");
}

// Update is called once per frame
void Update () {

}


Unity 3D小地图的制作

using UnityEngine;
using System.Collections;

public class Script09 : MonoBehaviour {

GameObject plane;
GameObject cube;
//大地图的宽度
float mapWidth;
//大地图的高度
float mapHeight;

float widthCheck;
float heightCheck;

float map_Cube_x = 0;
float map_Cube_y=0;

bool Keyup;
bool KeyDown;
bool KeyLeft;
bool KeyRight;

//小地图的背景贴图
public Texture map;

//小地图的主角贴图
public Texture map_Cube;

// Use this for initialization
void Start () {
plane = GameObject.Find ("Plane");
cube = GameObject.Find ("Cube");
float size_x = plane.GetComponent<MeshFilter> ().mesh.bounds.size.x;
float size_z = plane.GetComponent<MeshFilter> ().mesh.bounds.size.z;
float scale_x = plane.transform.localScale.x;
float scale_z = plane.transform.localScale.z;
mapWidth = size_x * scale_x;
mapHeight = size_z * scale_z;
widthCheck = mapWidth / 2;
heightCheck = mapHeight / 2;
Check ();
}

void Check()
{
float x = cube.transform.position.x;
float z = cube.transform.position.z;

if(x>=widthCheck)
{
x=widthCheck;

}
if(x<=-widthCheck)
{
x=-widthCheck;

}

if(z>=heightCheck)
{
z=heightCheck;

}
if(z<=-heightCheck)
{
z=-heightCheck;

}
cube.transform.position = new Vector3 (x,cube.transform.position.y,z);
map_Cube_x=(map.width/mapWidth*x)+((map.width/2)-(map_Cube.width/2))+(Screen.width-map.width);
map_Cube_y=map.height-((map.height/mapHeight*z)+(map.height/2));
}

// Update is called once per frame
void Update () {

}
void FixedUpdate()
{
if(Keyup)
{
cube.transform.Translate (-Vector3.forward*Time.deltaTime*5);
Check ();
}
if(KeyDown)
{
cube.transform.Translate (Vector3.forward*Time.deltaTime*5);
Check ();
}

if(KeyLeft)
{
cube.transform.Translate (Vector3.right*Time.deltaTime*5);
Check ();
}

if(KeyRight)
{
cube.transform.Translate (-Vector3.right*Time.deltaTime*5);
Check ();
}

}
void OnGUI()
{
Keyup = GUILayout.RepeatButton ("向前移动");

KeyDown = GUILayout.RepeatButton ("向后移动");

KeyLeft = GUILayout.RepeatButton ("向左移动");

KeyRight = GUILayout.RepeatButton ("向右移动");

GUI.DrawTexture (new Rect(Screen.width-map.width,0,map.width,map.height),map);
cube = GameObject.Find ("Cube");
float size_z = cube.GetComponent<MeshFilter> ().mesh.bounds.size.z;

float scale_z = cube.transform.localScale.z;

GUI.DrawTexture (new Rect(map_Cube_x,map_Cube_y-map_Cube.height/2,map_Cube.width,map_Cube.height),map_Cube);
print ("map_Cube_x"+map_Cube_x+"    "+"map_Cube_y"+map_Cube_y);
}

}


Unity 游戏运行时间和消耗时间

// Use this for initialization
void Start () {

}
void OnGUI()
{
GUILayout.Box ("游戏运行的时间:"+Time.time,GUILayout.Width (300));

GUILayout.Box ("执行上一帧消耗的时间:"+Time.deltaTime,GUILayout.Width (300));

GUILayout.Box ("固定时间:"+Time.fixedTime,GUILayout.Width (300));

GUILayout.Box ("上一帧消耗的固定时间:"+Time.fixedDeltaTime,GUILayout.Width (300));
}
// Update is called once per frame
void Update () {

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: