UNITY3D学习笔记8

来源:互联网 发布:洗发露有哪些牌子知乎 编辑:程序博客网 时间:2024/06/05 20:32






using UnityEngine;using System.Collections;public class TestA1 : MonoBehaviour {public Material mat;void OnPostRender(){DrawTriangle(100,0,100,200,200,100,mat);}void DrawTriangle(float x1,float y1,float x2,float y2,float x3,float y3,Material mat){mat.SetPass(0);GL.LoadOrtho();GL.Begin(GL.TRIANGLES);GL.Vertex3(x1/Screen.width,y1/Screen.height,0);GL.Vertex3(x2/Screen.width,y2/Screen.height,0);GL.Vertex3(x3/Screen.width,y3/Screen.height,0);GL.End();}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}}


using UnityEngine;using System.Collections;public class TestA2 : MonoBehaviour {public Material mat0;public Material mat1;public Material mat3;void OnPostRender(){DrawRect(100,100,100,100,mat0);DrawRect(100,100,100,100,mat1);DrawQuads(15,5,10,115,95,110,90,10,mat3);}void DrawRect(float x,float y,float width,float height,Material mat){GL.PushMatrix();mat.SetPass(0);GL.Begin(GL.QUADS);GL.Vertex3(x/Screen.width,y/Screen.height,0);GL.Vertex3(x/Screen.width,(y+height)/Screen.height,0);GL.Vertex3((x+width)/Screen.width,(y+height)/Screen.height,0);GL.Vertex3((x+width)/Screen.width,y/Screen.height,0);GL.End();GL.PopMatrix();}void DrawQuads(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4,Material mat){GL.PushMatrix();mat.SetPass(0);GL.Begin(GL.QUADS);GL.Vertex3(x1/Screen.width,y1/Screen.height,0);GL.Vertex3(x2/Screen.width,y1/Screen.height,0);GL.Vertex3(x3/Screen.width,y1/Screen.height,0);GL.Vertex3(x4/Screen.width,y1/Screen.height,0);GL.End();GL.PopMatrix();}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}}




using UnityEngine;using System.Collections;public class MoveCamera : MonoBehaviour {public Transform target;private float distance = 2.0f;float x;float y;float yMinLimit = -20.0f;float yMaxLimit = 80.0f;float xSpeed = 250.0f;float ySpeed = 120.0f;// Use this for initializationvoid Start () {Vector2 angles = transform.eulerAngles;x = angles.y;y = angles.x;if(rigidbody){rigidbody.freezeRotation = true;}}void LateUpdate(){if(target){x += Input.GetAxis("Mouse X")*xSpeed*0.02f;y -= Input.GetAxis("Mouse Y")*ySpeed*0.02f;y = ClampAngle(y,yMinLimit,yMaxLimit);Quaternion rotation  = Quaternion.Euler(y,x,0);Vector3 position = rotation * new Vector3(0.0f,0.0f,(-distance)) + target.position;transform.rotation = rotation;transform.position = position;}}float ClampAngle(float angle,float min,float max){if(angle<-360)angle += 360;if(angle>360)angle -= 360;return Mathf.Clamp(angle,min,max);}// Update is called once per framevoid Update () {}}



using UnityEngine;using System.Collections;public class TestA3 : MonoBehaviour {private GameObject LineRenderGameObject;private LineRenderer lineRenderer;private int lineLength = 4;private Vector3 v0 = new Vector3(1.0f,0.0f,0.0f);private Vector3 v1 = new Vector3(2.0f,0.0f,0.0f);private Vector3 v2 = new Vector3(3.0f,0.0f,0.0f);private Vector3 v3 = new Vector3(4.0f,0.0f,0.0f);// Use this for initializationvoid Start () {LineRenderGameObject = GameObject.Find("ObjLine");lineRenderer = (LineRenderer)LineRenderGameObject.GetComponent("LineRenderer");lineRenderer.SetVertexCount(lineLength);lineRenderer.SetWidth(0.1f,0.1f);}// Update is called once per framevoid Update () {lineRenderer.SetPosition(0,v0);lineRenderer.SetPosition(1,v1);lineRenderer.SetPosition(2,v2);lineRenderer.SetPosition(3,v3);}}



using UnityEngine;using System.Collections;public class TestA4 : MonoBehaviour {Vector3 v0 = new Vector3(5,0,0);Vector3 v1 = new Vector3(0,5,0);Vector3 v2 = new Vector3(0,0,5);Vector3 v3 = new Vector3(-5,0,0);Vector3 v4 = new Vector3(0,-5,0);Vector3 v5 = new Vector3(0,0,-5);Vector2 u0 = new Vector2(0,0);Vector2 u1 = new Vector2(0,5);Vector2 u2 = new Vector2(5,5);Vector2 u3 = new Vector2(0,0);Vector2 u4 = new Vector2(0,1);Vector2 u5 = new Vector2(1,1);// Use this for initializationvoid Start () {MeshFilter meshFilter = (MeshFilter)GameObject.Find("face").GetComponent(typeof(MeshFilter));Mesh mesh = meshFilter.mesh;mesh.vertices = new Vector3[]{v0,v1,v2,v3,v4,v5};mesh.uv = new Vector2[]{u0,u1,u2,u3,u4,u5};mesh.triangles = new int[]{0,1,2,3,4,5};}// Update is called once per framevoid Update () {}}



using UnityEngine;using System.Collections;public class TestA5 : MonoBehaviour {public const int HERO_UP = 0;public const int HERO_RIGHT = 1;public const int HERO_DOWN = 2;public const int HERO_LEFT = 3;public int state = 0;public int moveSpeed = 10;public void Awake(){state = HERO_DOWN;}// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {float KeyVertical = Input.GetAxis("Vertical");float KeyHorizontal = Input.GetAxis("Horizontal");if(KeyVertical == -1){setHeroState(HERO_LEFT);}else if(KeyVertical == 1){setHeroState(HERO_RIGHT);}if(KeyHorizontal == 1){setHeroState(HERO_DOWN);}else if(KeyHorizontal == -1){setHeroState(HERO_UP);}if(KeyVertical == 0 && KeyHorizontal == 0){//animation.Play();}}public void setHeroState(int newState){int rotateValue = (newState - state) * 90;Vector3 transformValue = new Vector3();//animation.Play("walk");switch(newState){case HERO_UP:transformValue = Vector3.forward * Time.deltaTime;break;case HERO_DOWN:transformValue = (-Vector3.forward) * Time.deltaTime;break;case HERO_LEFT:transformValue = Vector3.left * Time.deltaTime;break;case HERO_RIGHT:transformValue = (-Vector3.left) * Time.deltaTime;break;}transform.Rotate(Vector3.up,rotateValue);transform.Translate(transformValue*moveSpeed,Space.World);state = newState;}}



using UnityEngine;using System.Collections;public class TestA6 : MonoBehaviour {private string username = "";private string usernumber = "";private string userage = "";private string userheight = "";private bool showInfo = false;// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}void OnGUI(){GUILayout.BeginHorizontal("box",GUILayout.Width(200));GUILayout.Label("name");username = GUILayout.TextField(username,10);GUILayout.EndHorizontal();GUILayout.BeginHorizontal("box");GUILayout.Label("number");usernumber = GUILayout.TextField(usernumber,11);GUILayout.EndHorizontal();GUILayout.BeginHorizontal("box");GUILayout.Label("age");userage = GUILayout.TextField(userage,2);GUILayout.EndHorizontal();GUILayout.BeginHorizontal("box");GUILayout.Label("height");userheight = GUILayout.TextField(userheight,5);GUILayout.EndHorizontal();if(GUILayout.Button("submit")){showInfo = true;PlayerPrefs.SetString("username",username);PlayerPrefs.SetString("usernumber",usernumber);PlayerPrefs.SetInt("userage",int.Parse(userage));PlayerPrefs.SetFloat("userheight",float.Parse(userheight));}if(GUILayout.Button("canncel show")){showInfo = false;PlayerPrefs.DeleteAll();}if(showInfo){GUILayout.Label("username:"+PlayerPrefs.GetString("username","username"));GUILayout.Label("usernumber:"+PlayerPrefs.GetString("usernumber","usernumber"));GUILayout.Label("userage:"+PlayerPrefs.GetInt("userage",0).ToString());GUILayout.Label("userheight:"+PlayerPrefs.GetFloat("userheight",0.0f).ToString());}}}



using UnityEngine;using System.Collections;using System.IO;public class TestA7 : MonoBehaviour {// Use this for initializationvoid Start () {CreateFile(Application.dataPath,"FileName3","TestInfo0");CreateFile(Application.dataPath,"FileName4","TestInfo1");CreateFile(Application.dataPath,"FileName5","TestInfo2");}// Update is called once per framevoid Update () {}void CreateFile(string path,string name,string info){StreamWriter sw;FileInfo t = new FileInfo(path+"//"+name);if(!t.Exists){sw = t.CreateText();}else{sw = t.AppendText();}sw.WriteLine(info);sw.Close();sw.Dispose();}}



using UnityEngine;using System.Collections;using System.Collections.Generic;using System.IO;using System;public class TestA8 : MonoBehaviour {// Use this for initializationvoid Start () {ArrayList info = LoadFile(Application.dataPath,"FileName");foreach(string str in info){Debug.Log(str);}}ArrayList LoadFile(string path,string name){StreamReader sr = null;try{sr = File.OpenText(path+"//"+name);}catch(Exception e){return null;}string line;ArrayList arrList = new ArrayList();while((line = sr.ReadLine())!= null){arrList.Add(line);}sr.Close();sr.Dispose();return arrList;}// Update is called once per framevoid Update () {}}



using UnityEngine;using System.Collections;using System.Collections.Generic;using System.IO;using System;public class TestA9 : MonoBehaviour {ArrayList info = null;bool button = false;public Vector2 scrollPosition;// Use this for initializationvoid Start () {info = LoadFile(Application.dataPath,"FileName0");scrollPosition = new Vector2(0.0f,0.0f);}ArrayList LoadFile(string path,string name){StreamReader sr = null;try{sr = File.OpenText(path+"//"+name);}catch(Exception e){e.ToString();return null;}string line;ArrayList arrLise  = new ArrayList();while((line = sr.ReadLine())!=null){string[] str = line.Split(new char[]{'&'});foreach(string i in str){arrLise.Add(i);}}sr.Close();sr.Dispose();return arrLise;}// Update is called once per framevoid Update () {}void OnGUI(){scrollPosition = GUILayout.BeginScrollView(scrollPosition,                          GUILayout.Width(Screen.width),                          GUILayout.Height(Screen.height));GUILayout.BeginHorizontal("box");if(GUILayout.Button("one")){info = LoadFile(Application.dataPath,"FileName0");}if(GUILayout.Button("two")){info = LoadFile(Application.dataPath,"FileName1");}if(GUILayout.Button("three")){info = LoadFile(Application.dataPath,"FileName2");}if(GUILayout.Button("four")){info = LoadFile(Application.dataPath,"FileName3");}if(GUILayout.Button("five")){info = LoadFile(Application.dataPath,"FileName4");}GUILayout.EndHorizontal();if(GUILayout.Button("show/hide")){if(button)button = false;elsebutton = true;}int size = info.Count;for(int i = 0;i<size;i++){string text = (string)info[i];string title = text.Substring(0,3)+"...";GUILayout.Box(title);if(button){GUILayout.Label(text);}}GUILayout.EndScrollView();}}



using UnityEngine;using System.Collections;public class TestA10 : MonoBehaviour {// Use this for initializationvoid Start () { }// Update is called once per framevoid Update () { }void OnGUI(){GUILayout.Label("current scene:"+Application.loadedLevelName);if(GUILayout.Button("click in new Scene")){Application.LoadLevel("10");}}}



using UnityEngine;using System.Collections;public class TestA11 : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}void OnGUI(){GUILayout.Label("current scene:"+Application.loadedLevelName);if(GUILayout.Button("click ccc")){Application.CaptureScreenshot("name.png");}if(GUILayout.Button("open web")){Application.OpenURL("http://blog.csdn.net/laotou99");}if(GUILayout.Button("exit")){Application.Quit();}}}



using UnityEngine;using System.Collections;using UnityEditor;public class TestA12 : MonoBehaviour {Texture2D texture;// Use this for initializationvoid Start () {texture = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Texture/0.png",typeof(Texture2D));}// Update is called once per framevoid Update () { }void OnGUI(){GUI.DrawTexture(new Rect(0,0,texture.width,texture.height),texture);}}



using UnityEngine;using System.Collections;using UnityEditor;public class TestA13 : MonoBehaviour {Texture2D texture = null;// Use this for initializationvoid Start () {Material mat = new Material(Shader.Find("Transparent/Diffuse"));texture = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Texture/0.png",typeof(Texture2D));mat.mainTexture = texture;AssetDatabase.CreateAsset(mat,"Assets/mat.mat");GameObject objCube = GameObject.CreatePrimitive(PrimitiveType.Cube);objCube.renderer.material = mat;}// Update is called once per framevoid Update () {  }}



using UnityEngine;using System.Collections;using UnityEditor;public class TestA14 : MonoBehaviour {int addId = 0;// Use this for initializationvoid Start () { }// Update is called once per framevoid Update () { }void OnGUI(){if(GUILayout.Button("add folder")){string folder = "folderName"+addId;AssetDatabase.CreateFolder("Assets","folderName"+addId);Material mat = new Material(Shader.Find("Transparent/Diffuse"));AssetDatabase.CreateAsset(mat,"Assets/"+folder+"/mat.mat");addId++;}if(GUILayout.Button("Copy/Paste")){AssetDatabase.CopyAsset("Assets/folderName1/mat1.mat","Assets/folderName2/mat1.mat");AssetDatabase.MoveAsset("Assets/folderName0/mat0.mat","Assets/folderName2/mat0.mat");}if(GUILayout.Button("Delete")){AssetDatabase.DeleteAsset("Assets/folderName2/mat1.mat");AssetDatabase.Refresh();}}}



using UnityEngine;using System.Collections;using UnityEditor;public class TestA15 : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}void OnMouseDrag(){Debug.Log("mouse drag");}void OnMouseDown(){Debug.Log("mouse down");}void OnMouseUp(){Debug.Log("mouse up");}void OnMouseEnter(){Debug.Log("mouse in");}void OnMouseExit(){renderer.material.color =Color.white;}void OnMouseOver(){renderer.material.color =Color.red;}}



using UnityEngine;using System.Collections;using UnityEditor;public class TestA16 : MonoBehaviour {Texture2D texture = null;// Use this for initializationvoid Start () {Material mat = new Material(Shader.Find("Transparent/Diffuse"));texture = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Texture/0.png",typeof(Texture2D));mat.mainTexture = texture;AssetDatabase.CreateAsset(mat,"Assets/mat.mat");GameObject objCube = GameObject.CreatePrimitive(PrimitiveType.Cube);objCube.renderer.material = mat;objCube.AddComponent("Move");}// Update is called once per framevoid Update () {}}



using UnityEngine;using System.Collections;public class Move : MonoBehaviour {// Use this for initializationvoid Start () { }// Update is called once per framevoid Update () { }void OnMouseDrag(){transform.position += Vector3.right * Time.deltaTime*Input.GetAxis("Mouse X");transform.position += Vector3.up * Time.deltaTime*Input.GetAxis("Mouse Y");}void OnMouseDown(){Debug.Log("mouse down");}void OnMouseUp(){Debug.Log("mouse up");}void OnMouseEnter(){Debug.Log("mouse in");}void OnMouseExit(){renderer.material.color =Color.white;}void OnMouseOver(){renderer.material.color =Color.red;}}


using UnityEngine;using System.Collections;public class TestA17 : MonoBehaviour {// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {if(Input.GetMouseButtonDown(0)){Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;if(Physics.Raycast(ray,out hit)){GameObject objCube = GameObject.CreatePrimitive(PrimitiveType.Cube);objCube.transform.position = hit.point; objCube.AddComponent<Rigidbody>();}}}}





0 0