unity的NGUI(2)

1、Popup List(下拉列表)

创建一个Sprite–>添加Box Collider–>添加Popup List Script–>Options(大学、研究生、博士)–>设置Default、Positon、Alignment、Background–>为Sprite创建一个子Label–>把子Label拖拉到Sprite属性On Value Change下面的Notify里面–>Method选中setCurrentSelection

用系统自带的Popup List,要把Simple Popup List和Label的字体都设置为Unity和Dynamice(动态的),才可以显示中文

2、Toggle(多选框)

创建一个Sprite–>添加Box Collider–>添加Toggle Script–>为Sprite创建一个子Sprite(对钩图片)–>把子Sprite拖拉到Sprite属性"Sprite"里面–>设置Transitoin、Starting State

3、拖拽和调节组件大小

新建一个Sprite–>添加Box Collider–>添加Drag Object–>把Sprite拖拉到属性UIDrag Object的Target里面(Drag Effect:拖拽效果)

添加一个子Sprite–>添加Box Collider–>添加Drag Resize Script–>把Sprite拖拉到属性UIDrag Resize的Target里面–>Anchors设置(Type:Unified 四个方向都为自身)

UIDrag Resize里面的属性Pivot设置拖拽效果

4、Scroll Bar (滚动条)

新建一个Sprite–>添加Box Collider–>添加Drag Object–>把Sprite拖拉到属性UIDrag Object的Target里面(Drag Effect:拖拽效果)

添加一个子Sprite–>添加Box Collider–>添加Drag Resize Script–>把Sprite拖拉到属性UIDrag Resize的Target里面–>Anchors设置(Type:Unified 四个方向都为自身)

UIDrag Resize里面的属性Pivot设置拖拽效果

5、TextList

新建一个Label–>添加Box Collider–添加Text List–>添加脚本
private UITextList testList;
int i=0;
void Start () {
testList = this.GetComponent ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
i++;
testList.Add (“gopedu”+i);
}
}

6、背包系统

新建一个Sprite(鞋子)添加Box Collider添加Drag Drop Item添加3个Sprite(格子)设置鞋子的属性(Widget里面的Depth点击Forward并设置值为2(拖拉鞋子可以放到格子上面),把shoe拖拉到Sprite上面)

使用Json解析完成背包功能

删除UIDrag Drop Item,为格子添加Box Collider,创建一个脚本放到鞋子上,当把鞋子放到格子上时就可以看到控制台打印信息。

public class KnapsackItem : UIDragDropItem{

	protected override void OnDragDropRelease (GameObject surface)
	{
		base.OnDragDropRelease (surface);

		print (surface);
	}
}
修改脚本KnapsackItem ,当拖拉鞋子到各个格子里面时默认都居中

public class KnapsackItem : UIDragDropItem {

	protected override void OnDragDropRelease (GameObject surface)
	{
		base.OnDragDropRelease (surface);

		this.transform.parent = surface.transform;
		this.transform.localPosition = Vector3.zero;
	}
}
把shoe放入prefabs文件夹中,然后删掉shoe-->给每一个knapsack都添加Box Collider-->编写一个脚本然后拖拉到bg(背景)上面-->把bg属性Cells的Size大小设置为9-->把每个knapsack分别拖拉到对应的Element里面

public class MyKnapsack : MonoBehaviour {

	public GameObject[] cells;
}
交换鞋子
把两只鞋子(shoe)分别放到两个背包(knaspack)里面-->添加两个标签(格子的标签是Cell ,背包的标签是Knaspack)
public class KnapsackItem : UIDragDropItem {
	protected override void OnDragDropRelease (GameObject surface)
	{
		base.OnDragDropRelease (surface);
		if (surface.tag == "Cell") {
			this.transform.parent = surface.transform;
			this.transform.localPosition = Vector3.zero;
		}else if(surface.tag =="Knaspack"){
			Transform parent=surface.transform.parent;
			surface.transform.parent=this.transform.parent;
			surface.transform.localPosition=Vector3.zero;

			this.transform.parent=parent;
			this.transform.localPosition=Vector3.zero;
		}
	}
}
为objectName添加三个游戏物体,把预设shoe放入item
public class MyKnapsack : MonoBehaviour {
	public GameObject[] cells;
	public string[] objectName;
        public GameObject item;    
}
public class KnapsackItem : UIDragDropItem {

	public UISprite sprite;
	public UILabel label;
	private int count = 1;

	public void AddCount(int number){
		count += number;
		label.text = count + "";
	}

	protected override void OnDragDropRelease (GameObject surface)
	{
		base.OnDragDropRelease (surface);

		if (surface.tag == "Cell") {
			this.transform.parent = surface.transform;
			this.transform.localPosition = Vector3.zero;
		}else if(surface.tag =="Knapsack"){
			Transform parent=surface.transform.parent;
			surface.transform.parent=this.transform.parent;
			surface.transform.localPosition=Vector3.zero;

			this.transform.parent=parent;
			this.transform.localPosition=Vector3.zero;
		}
	}
}
public class MyKnapsack : MonoBehaviour {

	public GameObject[] cells;
	public string[] objectName;
	public GameObject item;

	void Update(){
		if (Input.GetKeyDown (KeyCode.X)) {
			Pickup();		
		}
	}
	public void Pickup(){
		int index = Random.Range (0,objectName.Length);
		string name = objectName [index];
      for (int i=0; i<cells.Length; i++) {
        if (cells [i].transform.childCount == 0) 
        {
	//如果当前位置没有物品,添加我们捡起来的物品
	GameObject go = NGUITools.AddChild (cells [i], item);
	go.GetComponent<UISprite> ().spriteName = name;
	go.transform.localPosition = Vector3.zero;
	break;			
          }else {
                KnapsackItem ki = cells[i].GetComponentInChildren<KnapsackItem>();
                if (ki.sprite.spriteName.Equals(name)) 
               {
                    ki.AddCount(1);
                    break;
            }
          } 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值