UNITY3D学习笔记7




using UnityEngine;
using System.Collections;

public class Test1 : MonoBehaviour {

	public Texture texture;

	private string info;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

		RaycastHit hit;

		if(Physics.Raycast(ray,out hit)){
			info = "OK";
		}else{
			info = "NO";
		}
	}

	void OnGUI(){

		Rect rect = new Rect(Input.mousePosition.x-(texture.width>>1),Screen.height - Input.mousePosition.y-(texture.height>>1),texture.width,texture.height);

		GUI.DrawTexture(rect,texture);

		GUILayout.Label(info+"坐标为:"+Input.mousePosition);
	}
}



using UnityEngine;
using System.Collections;

public class Test2 : MonoBehaviour {

	GameObject connectedObj = null;
	Component jointComponent = null;

	// Use this for initialization
	void Start () {
		connectedObj = GameObject.Find("Cube1");
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		if(GUILayout.Button("HingeJoint")){
			RestJoint();
			jointComponent = gameObject.AddComponent("HingeJoint");
			HingeJoint hjoint = (HingeJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			hjoint.connectedBody = connectedObj.rigidbody;
		}

		if(GUILayout.Button("FixedJoint")){
			RestJoint();
			jointComponent = gameObject.AddComponent("FixedJoint");
			FixedJoint fjoint = (FixedJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			fjoint.connectedBody = connectedObj.rigidbody;
		}
		
		if(GUILayout.Button("SpringJoint")){
			RestJoint();
			jointComponent = gameObject.AddComponent("SpringJoint");
			SpringJoint sjoint = (SpringJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			sjoint.connectedBody = connectedObj.rigidbody;
		} 
		
		if(GUILayout.Button("CharacterJoint")){
			RestJoint();
			jointComponent = gameObject.AddComponent("CharacterJoint");
			CharacterJoint cjoint = (CharacterJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			cjoint.connectedBody = connectedObj.rigidbody;
		}
		
		if(GUILayout.Button("ConfigurableJoint")){
			RestJoint();
			jointComponent = gameObject.AddComponent("ConfigurableJoint");
			ConfigurableJoint cojoint = (ConfigurableJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			cojoint.connectedBody = connectedObj.rigidbody;
		} 
	}

	void RestJoint(){
		Destroy(jointComponent);
		//this.transform.position = new Vector3(821.0f,72.0f,660.0f);
		//connectedObj.gameObject.transform.position = new Vector3(805.0f,48.0f,660.0f);

		this.transform.position = new Vector3(5.0f,15.0f,5.0f);
		connectedObj.gameObject.transform.position = new Vector3(10.0f,20.0f,10.0f);

		connectedObj.rigidbody.useGravity = false;
	}
}



using UnityEngine;
using System.Collections;

public class Test3 : MonoBehaviour {

	GameObject particle = null;

	float velocity_x = 0.0f;
	float velocity_y = 0.0f;
	float velocity_z = 0.0f;

	// Use this for initialization
	void Start () {
		particle = GameObject.Find("ParticleSystem");

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

	void OnGUI(){
		GUILayout.Label("粒子最大尺寸");
		particle.particleEmitter.maxSize = GUILayout.HorizontalSlider(
			particle.particleEmitter.maxSize,0.0f,10.0f,GUILayout.Width(150));

		GUILayout.Label("粒子消失时间");
		particle.particleEmitter.maxEnergy = GUILayout.HorizontalSlider(
			particle.particleEmitter.maxEnergy,0.0f,10.0f,GUILayout.Width(150));
		
		GUILayout.Label("粒子的最大生成数量");
		particle.particleEmitter.maxEmission = GUILayout.HorizontalSlider(
			particle.particleEmitter.maxEmission,0.0f,10.0f,GUILayout.Width(150));

		GUILayout.Label("粒子X轴的移动速度");
		velocity_x = GUILayout.HorizontalSlider(velocity_x,0.0f,10.0f,GUILayout.Width(150));
		particle.particleEmitter.worldVelocity = 
			new Vector3(velocity_x,particle.particleEmitter.worldVelocity.y,particle.particleEmitter.worldVelocity.z);

		GUILayout.Label("粒子Y轴的移动速度");
		velocity_y = GUILayout.HorizontalSlider(velocity_y,0.0f,10.0f,GUILayout.Width(150));
		particle.particleEmitter.worldVelocity = 
			new Vector3(particle.particleEmitter.worldVelocity.x,velocity_y,particle.particleEmitter.worldVelocity.z);

		GUILayout.Label("粒子Z轴的移动速度");
		velocity_z = GUILayout.HorizontalSlider(velocity_z,0.0f,10.0f,GUILayout.Width(150));
		particle.particleEmitter.worldVelocity = 
			new Vector3(particle.particleEmitter.worldVelocity.x,particle.particleEmitter.worldVelocity.y,velocity_z);
		

	}
}



using UnityEngine;
using System.Collections;

public class Test4 : MonoBehaviour {

	Cloth cloth = null;

	// Use this for initialization
	void Start () {
		cloth = (Cloth)GetComponent<InteractiveCloth>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		if(GUILayout.RepeatButton("UP")){
			cloth.externalAcceleration = new Vector3(0,11,0);
		}

		if(GUILayout.RepeatButton("DOWN")){
			cloth.externalAcceleration = new Vector3(0,-11,0);
		}

		if(GUILayout.RepeatButton("LEFT")){
			cloth.externalAcceleration = new Vector3(11,0,0);
		}

		if(GUILayout.RepeatButton("RIGHT")){
			cloth.externalAcceleration = new Vector3(-11,0,0);
		}
	}
}



using UnityEngine;
using System.Collections;

public class Test5 : MonoBehaviour {

	private TrailRenderer trialRender;

	// Use this for initialization
	void Start () {
		trialRender = gameObject.GetComponent<TrailRenderer>();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnGUI(){
		if(GUILayout.Button("add width",GUILayout.Height(50))){
			trialRender.startWidth += 1;
			trialRender.endWidth +=1;
		}

		if(GUILayout.Button("show path",GUILayout.Height(50))){
			trialRender.enabled = true;
		}

		if(GUILayout.Button("hide path",GUILayout.Height(50))){
			trialRender.enabled = false;
		}
	}
}



using UnityEngine;
using System.Collections;

public class Test6 : MonoBehaviour {

	private GameObject obj;
	public Texture texture;

	// Use this for initialization
	void Start () {
		obj = GameObject.Find("Sphere");

		Screen.showCursor = false;
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void FixedUpdate(){
		if(Input.GetMouseButton(0)){
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;

			if(Physics.Raycast(ray,out hit))
			{
				if(hit.collider.name == "Cube")
				{
					Vector3 direction = hit.transform.position - obj.transform.position;

					obj.rigidbody.AddForceAtPosition(direction,hit.transform.position,ForceMode.Impulse);
				}
			}
		}
	}

	void OnGUI(){
		Rect rect = 
			new Rect(Input.mousePosition.x - (texture.width >>1),
			         Screen.height - Input.mousePosition.y-(texture.height >>1),
			         texture.width,texture.height);

		GUI.DrawTexture(rect,texture);
	}
}



using UnityEngine;
using System.Collections;

public class Test7 : MonoBehaviour {

	int keyFrame = 0;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.W))
		{
			Debug.Log ("W");
		}

		if(Input.GetKey(KeyCode.W))
		{
			Debug.Log ("W");
		}

		if(Input.GetKeyUp(KeyCode.W))
		{
			Debug.Log ("W");
		}

		if(Input.GetKeyDown(KeyCode.S))
		{
			Debug.Log ("S");
		}

		if(Input.GetKeyDown(KeyCode.A))
		{
			Debug.Log ("A");
		}

		if(Input.GetKeyDown(KeyCode.D))
		{
			Debug.Log ("D");
		}

		if(Input.GetKeyDown(KeyCode.Space))
		{
			Debug.Log ("Space");
		}

		if(Input.anyKeyDown)
		{
			keyFrame = 0;
			Debug.Log ("keyFrame:"+keyFrame);
		}

		if(Input.anyKey)
		{
			keyFrame++;
			Debug.Log ("keyFrame:"+keyFrame);
		}
	}
}



using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test8 : MonoBehaviour {

	public Texture imageUp;
	public Texture imageDown;
	public Texture imageLeft;
	public Texture imageRight;
	public Texture imageSuccess;

	public const int KEY_UP =0;
	public const int KEY_DOWN =1;
	public const int KEY_LEFT =2;
	public const int KEY_RIGHT =3;
	public const int KEY_FIRT =4;

	public const int FRAME_COUNT  = 100;
	public const int SAMPLE_SIZE = 3;
	public const int SAMPLE_COUNT = 5;

	int[,] Sample = {
		{KEY_DOWN,KEY_RIGHT,KEY_DOWN,KEY_RIGHT,KEY_FIRT},
		{KEY_DOWN,KEY_RIGHT,KEY_DOWN,KEY_LEFT,KEY_FIRT},
		{KEY_DOWN,KEY_LEFT,KEY_DOWN,KEY_LEFT,KEY_FIRT}
	};

	int currentkeyCode = 0;

	bool startFrame = false;

	int currentFrame = 0;

	List<int> playerSample;

	bool isSuccess = false;
	// Use this for initialization
	void Start () {
		playerSample = new List<int>();
	}
	
	// Update is called once per frame
	void Update () {
		UpdateKey();
		if(Input.anyKeyDown)
		{
			if(isSuccess){
				isSuccess = false;
				Reset();
			}
			if(!startFrame){
				startFrame =  true;

			}

			playerSample.Add(currentkeyCode);

			int size = playerSample.Count;
			if(size==SAMPLE_COUNT)
			{
				for(int i = 0;i<SAMPLE_SIZE;i++){
					int SuccessCount = 0;
					for(int j = 0;j<SAMPLE_COUNT;j++){
						int temp = playerSample[j];
						if(temp == Sample[i,j]){
							SuccessCount++;
						}
					}

					if(SuccessCount == SAMPLE_COUNT){
						isSuccess = true;
						break;
					}
				}
			}
		}

		if(startFrame){
			currentFrame++;
		}

		if(currentFrame>=FRAME_COUNT){
			if(!isSuccess){
				Reset();
			}
		}
	}

	void Reset(){
		currentFrame = 0;
		startFrame = false;
		playerSample.Clear();
	}

	void UpdateKey(){
		if(Input.GetKeyDown(KeyCode.W)){
			currentkeyCode  = KEY_UP;
		}
		if(Input.GetKeyDown(KeyCode.S)){
			currentkeyCode  = KEY_DOWN;
		}
		if(Input.GetKeyDown(KeyCode.A)){
			currentkeyCode  = KEY_LEFT;
		}
		if(Input.GetKeyDown(KeyCode.D)){
			currentkeyCode  = KEY_RIGHT;
		}
		if(Input.GetKeyDown(KeyCode.Space)){
			currentkeyCode  = KEY_FIRT;
		}
	}

	void OnGUI(){
		int size = playerSample.Count;

		for(int i = 0;i<size;i++)
		{
			int key = playerSample[i];
			Texture temp = null;
			switch(key){
			case KEY_UP:
				temp = imageUp;
				break;
			case KEY_DOWN:
				temp = imageDown;
				break;
			case KEY_LEFT:
				temp = imageLeft;
				break;
			case KEY_RIGHT:
				temp = imageRight;
				break;
			}
			if(temp!=null){
				GUILayout.Label(temp);
			}
		}

		if(isSuccess){
			GUILayout.Label(imageSuccess);
		}

		GUILayout.Label("1,下,前,下,前,拳");
		GUILayout.Label("2,下,前,下,后,拳");
		GUILayout.Label("3,下,后,下,后,拳");
	}
}



using UnityEngine;
using System.Collections;

public class Test9 : MonoBehaviour {
	int MouseFrame = 0;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		if(Input.GetMouseButtonDown(0)){
			Debug.Log("mousePosition:"+Input.mousePosition);
		}

		if(Input.GetMouseButtonDown(1)){
			Debug.Log("mousePosition:"+Input.mousePosition);
		}

		if(Input.GetMouseButtonDown(2)){
			Debug.Log("mousePosition:"+Input.mousePosition);
		}

		if(Input.GetMouseButtonUp(0)){
			Debug.Log("mousePosition:"+Input.mousePosition);
			MouseFrame = 0;
		}
		
		if(Input.GetMouseButtonUp(1)){
			Debug.Log("mousePosition:"+Input.mousePosition);
			MouseFrame = 0;
		}
		
		if(Input.GetMouseButtonUp(2)){
			Debug.Log("mousePosition:"+Input.mousePosition);
			MouseFrame = 0;
		}

		if(Input.GetMouseButton(0)){
			MouseFrame++;
			Debug.Log("MouseFrame:"+MouseFrame+" FPS");
		}
		if(Input.GetMouseButton(1)){
			MouseFrame++;
			Debug.Log("MouseFrame:"+MouseFrame+" FPS");
		}
		if(Input.GetMouseButton(2)){
			MouseFrame++;
			Debug.Log("MouseFrame:"+MouseFrame+" FPS");
		}
	}
}



using UnityEngine;
using System.Collections;

public class Test10 : MonoBehaviour {

	public Transform target;
	public float distance = 20.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")*xSpeed * 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 Test11 : MonoBehaviour {

	public const string ANIM_NAME0 = "idle";
	public const string ANIM_NAME1 = "run";
	public const string ANIM_NAME2 = "walk";
	public const string ANIM_NAME3 = "jump_pose";

	private GameObject obj = null;
	// Use this for initialization
	void Start () {
		obj = GameObject.Find("3rd Person Controller");
		obj.animation.wrapMode = WrapMode.Loop;
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyDown(KeyCode.A)){
			obj.animation.Play(ANIM_NAME0);
		}
		if(Input.GetKeyDown(KeyCode.B)){
			obj.animation.Play(ANIM_NAME1);
		}
		if(Input.GetKeyDown(KeyCode.C)){
			obj.animation.Play(ANIM_NAME2);
		}
		if(Input.GetKeyDown(KeyCode.D)){
			obj.animation.Play(ANIM_NAME3);
		}
	}

	void OnGUI(){
		GUILayout.Label("A B C D");
	}
}



using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Test12 : MonoBehaviour {

	public Material material;
	private List<Vector3> lineInfo;

	// Use this for initialization
	void Start () {
		lineInfo = new List<Vector3>();
	}
	
	// Update is called once per frame
	void Update () {
		lineInfo.Add (Input.mousePosition);
	}

	void OnGUI(){
		GUILayout.Label("Input.mousePosition.x:"+Input.mousePosition.x);
		GUILayout.Label("Input.mousePosition.y:"+Input.mousePosition.y);
	}

	void OnPostRender(){
		if(!material){
			Debug.LogError("error");
			return;
		}

		material.SetPass(0);
		GL.LoadOrtho();
		GL.Begin(GL.LINES);

		int size = lineInfo.Count;
		for(int i = 0;i<size -1;i++){
			Vector3 start = lineInfo[i];
			Vector3 end = lineInfo[i+1];
			DrawLine(start.x,start.y,end.x,end.y);
		}

		GL.End();
	}

	void DrawLine(float x1,float y1,float x2,float y2){
		GL.Vertex(new Vector3(x1/Screen.width,y1/Screen.height,0));
		GL.Vertex(new Vector3(x2/Screen.width,y2/Screen.height,0));
	}
}



using UnityEngine;
using System.Collections;

public class Test13 : MonoBehaviour {

	public Material mat0;
	public Material mat1;
	public Material mat3;

	void OnPostRender(){
		DrawRect(100,100,100,100,mat0);
		DrawRect(250,100,100,100,mat1);
		DrawQuads(15,5,10,115,95,110,90,10,mat3);
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void DrawRect(float x,float y,float width,float height,Material mat){
		GL.PushMatrix();
		mat.SetPass(0);
		GL.LoadOrtho();
		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.LoadOrtho();

		GL.Begin(GL.QUADS);
		GL.Vertex3(x1/Screen.width,y1/Screen.height,0);
		GL.Vertex3(x1/Screen.width,y1/Screen.height,0);
		GL.Vertex3(x1/Screen.width,y1/Screen.height,0);
		GL.Vertex3(x1/Screen.width,y1/Screen.height,0);

		GL.End();
		GL.PopMatrix();
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值