Unity3DGame学习笔记:背包系统(homework10)

本次作业的任务是使用UI制作一个背包系统

首先要制作一个静态的场景

游戏对象如下

Unity3DGame学习笔记:背包系统(homework10)_第1张图片

其中Bag是新建的空对象,在里面创建9个按钮,下面是Bag和其按钮的参数设置

Unity3DGame学习笔记:背包系统(homework10)_第2张图片

Unity3DGame学习笔记:背包系统(homework10)_第3张图片

Equipment为装备栏,在里面创建3个按钮,下面是装备栏和其按钮的参数设置

Unity3DGame学习笔记:背包系统(homework10)_第4张图片

Unity3DGame学习笔记:背包系统(homework10)_第5张图片

Random是一个按钮,用于重新生成装备栏中的装备

再加上人物预设(从Asset Store上下载),摄像机,粒子系统,背景图片

下面是UI Camera的参数设置

Unity3DGame学习笔记:背包系统(homework10)_第6张图片

以及Hero Camera的参数设置,人物预设设置为HeroCamera的child

Unity3DGame学习笔记:背包系统(homework10)_第7张图片

调整相应位置后可得到如下的场景

Unity3DGame学习笔记:背包系统(homework10)_第8张图片

至此已经完成场景的基本布局

接下来的任务是用代码实现具体的UI行为

首先是TiltWindow.cs,挂载在对象Bag和Equipment上,用于实现画布随鼠标摆动

public class TiltWindow : MonoBehaviour
{
	public Vector2 range = new Vector2(5f, 3f);

	Transform mTrans;
	Quaternion mStart;
	Vector2 mRot = Vector2.zero;

	void Start ()
	{
		mTrans = transform;
		mStart = mTrans.localRotation;
	}

	void Update ()
	{
		Vector3 pos = Input.mousePosition;

		float halfWidth = Screen.width * 0.5f;
		float halfHeight = Screen.height * 0.5f;
		float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
		float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
		mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f);

		mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, -mRot.x * range.x, 0f);
	}
}


接下来是GameManager.cs,挂载在对象Canvas上,用于作为场景的控制管理器

其中三个int参数主要用于标志装备栏中的装备,通过将他们设置为1,具体在下面Equipment.cs中的Update函数通过检测这三个数的值重新生成新的装备

public class GameManager : MonoBehaviour {

	private MouseImage mouse;
	private int IsHat = 0;
	private int IsHand = 0;
	private int IsShoes = 0;

	void Start() {
		mouse = (MouseImage)FindObjectOfType (typeof(MouseImage));
	}

	public MouseImage getMouse() {
		return mouse;
	}

	public void setMouse(MouseImage m) {
		if (mouse == null) {
			mouse = m;
		}
	}

	public void RandomWear() {
		IsHat = 1;
		IsHand = 1;
		IsShoes = 1;
	}

	public int getHat() { return IsHat; }
	public int getHand() { return IsHand; }
	public int getShoes() { return IsShoes; }

	public void setHat(int h) { IsHat = h; }
	public void setHand(int h) { IsHand = h; }
	public void setShoes(int s) { IsShoes = s; }
}


接下来是Equipment.cs,挂载在每个equipment按钮上,用于完成装备栏中拾取和放置装备的行为

mouseType用于记录当前鼠标拾取的装备种类

OnEquipButton函数完成装备从装备栏到鼠标或者从鼠标到装备栏的移动

public class Equipment : MonoBehaviour {

	private GameManager GM;
	private Image equipImage;
	public int mouseType;
	public Sprite equip;
	public Sprite UISprite;

	void Start() {
		GM = (GameManager)FindObjectOfType (typeof(GameManager));
		equipImage = GetComponent ();
	}

	public void OnEquipButton() {
		int _mouseType = GM.getMouse ().getMouseType ();
		if (equipImage.sprite == equip && _mouseType == 0) {
			equipImage.sprite = UISprite;
			GM.getMouse ().setMouseType (mouseType);
		}
		else if (equipImage.sprite == UISprite && _mouseType == mouseType) {
			equipImage.sprite = equip;
			GM.getMouse ().setMouseType (0);
		}
	}

	void Update() {
		if (mouseType == 1 && GM.getHat () == 1) {
			GM.setHat (0);
			equipImage.sprite = equip;
		}
		else if (mouseType == 2 && GM.getHand() == 1)
		{
			GM.setHand (0);
			equipImage.sprite = equip;
		}
		else if (mouseType == 3 && GM.getShoes() == 1)
		{
			GM.setShoes (0);
			equipImage.sprite = equip;
		}
	}
}


由于装备栏中三个按钮规定的装备种类不同,因此在参数设置上也会有所不同,下面是用于放置Hat的按钮设置

Unity3DGame学习笔记:背包系统(homework10)_第9张图片


接下来是Bag.cs,挂载在Bag中每个按钮上,用于完成背包栏中的拾取和放置装备的行为

第一个if中实现背包为空,鼠标不为空的情况的放置

下面三个if中实现背包不为空的情况的拾取放置,当鼠标和背包中都有装备时,点击将使两者的装备互换

public class Bag : MonoBehaviour {

	private GameManager GM;
	private Image bagImage;
	private int mouseType = 0;

	public Sprite UISprite;
	public Sprite hat;
	public Sprite hand;
	public Sprite shoes;

	void Start() {
		GM = (GameManager)FindObjectOfType (typeof(GameManager));
		bagImage = GetComponent ();
	}

	public void OnBagButton() {
		int _mouseType = GM.getMouse ().getMouseType ();
		Debug.Log (_mouseType);
		if (bagImage.sprite == UISprite && _mouseType != 0) {
			Debug.Log ("put in");
			if (_mouseType == 1) {
				bagImage.sprite = hat;
				GM.getMouse ().setMouseType (0);
			} else if (_mouseType == 2) {
				bagImage.sprite = hand;
				GM.getMouse ().setMouseType (0);
			} else if (_mouseType == 3) {
				bagImage.sprite = shoes;
				GM.getMouse ().setMouseType (0);
			}
		}
		else if (bagImage.sprite == hat)
		{
			if (_mouseType == 0) {
				bagImage.sprite = UISprite;
			}
			else if (_mouseType == 1)
			{
				bagImage.sprite = hat;
			}
			else if (_mouseType == 2)
			{
				bagImage.sprite = hand;
			}
			else if (_mouseType == 3)
			{
				bagImage.sprite = shoes;
			}
			GM.getMouse ().setMouseType (1);
		}
		else if (bagImage.sprite == hand)
		{
			if (_mouseType == 0) {
				bagImage.sprite = UISprite;
			}
			else if (_mouseType == 1)
			{
				bagImage.sprite = hat;
			}
			else if (_mouseType == 2)
			{
				bagImage.sprite = hand;
			}
			else if (_mouseType == 3)
			{
				bagImage.sprite = shoes;
			}
			GM.getMouse ().setMouseType (2);
		}
		else if (bagImage.sprite == shoes)
		{
			if (_mouseType == 0) {
				bagImage.sprite = UISprite;
			}
			else if (_mouseType == 1)
			{
				bagImage.sprite = hat;
			}
			else if (_mouseType == 2)
			{
				bagImage.sprite = hand;
			}
			else if (_mouseType == 3)
			{
				bagImage.sprite = shoes;
			}
			GM.getMouse ().setMouseType (3);
		}
	}
}


由于背包中每个按钮并没有规定放置的装备种类,因此三种装备都可以放置

下面为Bag中按钮的参数设置

Unity3DGame学习笔记:背包系统(homework10)_第10张图片


接下来是MouseImage.cs,用于实现鼠标的拾取和放置行为,挂载在空的Image对象UI Image上

public class MouseImage : MonoBehaviour {

	private GameManager GM;
	private Image mouseImage;
	private int mouseType = 0;
	private int zPosition;

	public Sprite none;
	public Sprite hat;
	public Sprite hand;
	public Sprite shoes;
	public Camera cam;

	void Start() {
		GM = (GameManager)FindObjectOfType (typeof(GameManager));
		GM.setMouse (this);
		mouseImage = GetComponent ();
		zPosition = -400;
	}

	public int getMouseType() { return mouseType; }
	public void setMouseType(int m) { mouseType = m; }

	void Update() {
		Debug.Log (mouseType);
		if (mouseType == 0) {
			mouseImage.sprite = none;
		}
		else if (mouseType == 1) {
			mouseImage.sprite = hat;
		} 
		else if (mouseType == 2) {
			mouseImage.sprite = hand;
		} 
		else if (mouseType == 3) {
			mouseImage.sprite = shoes;
		}
		transform.position = new Vector3 (Input.mousePosition.x - 450, Input.mousePosition.y - 300 , zPosition);

		//Debug.Log (Input.mousePosition);
		if (mouseType != 0) {
			zPosition = 100;
		} else {
			zPosition = -400;
		}
	}
}


其中

transform.position = new Vector3 (Input.mousePosition.x - 450, Input.mousePosition.y - 300 , zPosition);


实现拾取的图片跟随鼠标移动

参数设置如下

Unity3DGame学习笔记:背包系统(homework10)_第11张图片

最后是RandomWear.cs,实现在装备栏中重新生成新的装备(调用GameManager中的函数),挂载在RandomWear按钮上

public class RandomWear : MonoBehaviour {

	private GameManager GM;

	void Start() {
		GM = (GameManager)FindObjectOfType (typeof(GameManager));
	}

	public void OnPress() {
		GM.RandomWear ();
	}
}


至此已基本完成,实现效果如下

Unity3DGame学习笔记:背包系统(homework10)_第12张图片(图片大小限制只能截这么小的图)

这次还有一些没有实现,如每种装备有多个不同的对象,在人物身上装上相应的装备,只能等以后有时间再完善了= =

你可能感兴趣的:(Unity3DGame学习笔记:背包系统(homework10))