UNITY3D学习笔记8






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 initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void 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 initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void 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 initialization
	void 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 frame
	void 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 initialization
	void Start () {
		LineRenderGameObject = GameObject.Find("ObjLine");
		lineRenderer = (LineRenderer)LineRenderGameObject.GetComponent("LineRenderer");
		lineRenderer.SetVertexCount(lineLength);
		lineRenderer.SetWidth(0.1f,0.1f);
	}
	
	// Update is called once per frame
	void 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 initialization
	void 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 frame
	void 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 initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void 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 initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void 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 initialization
	void Start () {
		CreateFile(Application.dataPath,"FileName3","TestInfo0");
		CreateFile(Application.dataPath,"FileName4","TestInfo1");
		CreateFile(Application.dataPath,"FileName5","TestInfo2");
	}
	
	// Update is called once per frame
	void 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 initialization
	void 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 frame
	void 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 initialization
	void 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 frame
	void 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;
			else
				button = 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 initialization
	void Start () { }
	
	// Update is called once per frame
	void 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 initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void 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 initialization
	void Start () {
		texture = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/Texture/0.png",typeof(Texture2D));
	}
	
	// Update is called once per frame
	void 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 initialization
	void 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 frame
	void Update () {  }
}



using UnityEngine;
using System.Collections;
using UnityEditor;

public class TestA14 : MonoBehaviour {

	int addId = 0;

	// Use this for initialization
	void Start () { }
	
	// Update is called once per frame
	void 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 initialization
	void Start () {	}
	
	// Update is called once per frame
	void 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 initialization
	void 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 frame
	void Update () {
	
	}
}



using UnityEngine;
using System.Collections;


public class Move : MonoBehaviour {


	// Use this for initialization
	void Start () { }
	
	// Update is called once per frame
	void 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 initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习Unity3D时,以下是一些重要的笔记: 1. Unity3D基础知识: - 游戏对象(Game Objects)和组件(Components):了解游戏对象的层次结构和组件的作用。 - 场景(Scenes)和摄像机(Cameras):学会如何创建场景并设置摄像机视角。 - 材质(Materials)和纹理(Textures):掌握如何创建和应用材质和纹理。 - 动画(Animations):学习如何创建和控制游戏对象的动画。 2. 脚本编程: - C#语言基础:了解C#语言的基本语法和面向对象编程概念。 - Unity脚本编写:学习如何编写脚本来控制游戏对象的行为和交互。 - 常见组件和功能:掌握常见的Unity组件和功能,如碰撞器(Colliders)、刚体(Rigidbodies)、触发器(Triggers)等。 3. 游戏开发流程: - 设计游戏关卡:了解如何设计游戏场景和关卡,包括布局、道具、敌人等。 - 游戏逻辑实现:将游戏规则和玩家交互转化为代码实现。 - UI界面设计:学习如何设计游戏中的用户界面,包括菜单、计分板等。 - 游戏优化和调试:优化游戏性能,解决常见的错误和问题。 4. 学习资源: - Unity官方文档和教程:官方提供了大量的文档和教程,逐步引导你学习Unity3D。 - 在线教程和视频教程:网上有很多免费和付费的Unity教程和视频教程,可根据自己的需求选择学习。 - 社区论坛和博客:加入Unity开发者社区,与其他开发者交流并获取帮助。 通过系统地学习这些内容,你将能够掌握Unity3D的基础知识并开始开发自己的游戏项目。记得不断实践和尝试,不断提升自己的技能!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值