NGUI-学习笔记(2)一个项目需求
using UnityEngine;
using System.Collections; public class ins1 : MonoBehaviour
{ //bool isTarget = false;
private Vector3 world;
private Vector3 screenpos;
private Vector3 mousepos;
private Vector3 offset;
public RaycastHit hit;
private Vector3 obj_pos;
private GameObject obj;
//private int speed;
//LayerMask mask_3 = 1 << 10;
//LayerMask mask_2 = 11 << 13;
// Use this for initialization
void Start()
{
StartCoroutine(move());
}
void Update()
{
if (Input.GetMouseButtonDown())
{
Ray();
}
}
IEnumerator move()
{
screenpos = Camera.main.WorldToScreenPoint(this.transform.position); offset = this.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenpos.z));
while (true)
{
mousepos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenpos.z);
world = Camera.main.ScreenToWorldPoint(mousepos) + offset;
this.transform.position = world;
yield return new WaitForFixedUpdate();
}
}
public void Ray()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//RaycastHit hit;
GameObject hitobj = null;
if (Physics.Raycast(ray, out hit))
{
hitobj = hit.collider.gameObject;
print(hitobj.name); if (hit.collider.gameObject.name == "plane")
{
obj_pos = hit.point;
obj = (GameObject)Instantiate(this.gameObject);
obj.transform.position = obj_pos;
Destroy(obj.GetComponent<ins1>());
Destroy(this.gameObject);
}
}
else
Destroy(this.gameObject); } }
主要实现选择label 显示图片 拖拽物体的功能。
预览:
过程:(1)添加label attach boxcollider UIbutton
添加sprite 如图
还有plane
(2)为label绑定脚本
using UnityEngine;
using System.Collections; public class getText : MonoBehaviour { private UILabel label;
public UIAtlas atlass;
public UISprite sprite;
public GameObject[] cells; // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { }
public void get()
{
label=this.GetComponent<UILabel>();
show(label.text);
}
public void show(string name)
{
for (int i = 0; i < cells.Length; i++)
{
if (cells[i].GetComponent<UISprite>().spriteName.Equals("Right Bracket"))//图集默认图片 {
sprite = cells[i].GetComponent<UISprite>(); sprite.atlas = atlass;
sprite.spriteName = name;
print(name);
break;
}
} }
}
为sprite绑定脚本
using UnityEngine;
using System.Collections; public class listen : MonoBehaviour
{
public GameObject[] obj;
private Vector3 screenpos;
private Vector3 mousepos;
private Vector3 world; // Use this for initialization
void Start()
{ } // Update is called once per frame
void Update()
{ }
void Awake()
{
UIEventListener.Get(this.gameObject).onClick += buttonclick;
} void buttonclick(GameObject button)
{
screenpos = Camera.main.WorldToScreenPoint(this.gameObject.transform.position);
mousepos = Input.mousePosition;
mousepos.z = screenpos.z;
world = Camera.main.ScreenToWorldPoint(mousepos);
string name = button.GetComponent<UISprite>().spriteName;
for (int i = 0; i < obj.Length; i++)
{
if (name == obj[i].name)
{ GameObject product = (GameObject)Instantiate(obj[i]);
product.transform.position = world; this.GetComponent<UISprite>().spriteName = "Right Bracket";
product.tag = "obj";
product.layer =10;
product.AddComponent<ins1>();
} }
}
}
ins1脚本:
NGUI-学习笔记(2)一个项目需求的更多相关文章
- django学习笔记二:一个项目多个App项目搭建
django充许在一个项目中存在多个app,如一个大门户网站中可以包含论坛,新闻等内容,其中每一个模块称之为一个App,也可以理解为一个个独立的小型项目最终集成在一个门户网站中最终呈现给用户 本次测试 ...
- ASP.NET MVC Web API 学习笔记---第一个Web API程序
http://www.cnblogs.com/qingyuan/archive/2012/10/12/2720824.html GetListAll /api/Contact GetListBySex ...
- NGUI学习笔记汇总
NGUI学习笔记汇总,适用于NGUI2.x,NGUI3.x 一.NGUI的直接用法 1. Attach a Collider:表示为NGUI的某些物体添加碰撞器,如果界面是用NGUI做的,只能这样添加 ...
- NGUI 学习笔记实战之二——商城数据绑定(Ndata)
上次笔记实现了游戏商城的UI界面,没有实现动态数据绑定,所以是远远不够的.今天采用NData来做一个商城. 如果你之前没看过,可以参考上一篇博客 NGUI 学习笔记实战——制作商城UI界面 ht ...
- NGUI 学习笔记实战——制作商城UI界面
http://www.cnblogs.com/chongxin/p/3876575.html Unity3D的uGUI听说最近4.6即将推出,但是目前NGUI等UI插件大行其道并且已经非常成熟,所以我 ...
- golang学习笔记6 beego项目路由设置
golang学习笔记5 beego项目路由设置 前面我们已经创建了 beego 项目,而且我们也看到它已经运行起来了,那么是如何运行起来的呢?让我们从入口文件先分析起来吧: package main ...
- Docker技术入门与实战 第二版-学习笔记-10-Docker Machine 项目-2-driver
1>使用的driver 1〉generic 使用带有SSH的现有VM/主机创建机器. 如果你使用的是机器不直接支持的provider,或者希望导入现有主机以允许Docker Machine进行管 ...
- Scrapy:学习笔记(2)——Scrapy项目
Scrapy:学习笔记(2)——Scrapy项目 1.创建项目 创建一个Scrapy项目,并将其命名为“demo” scrapy startproject demo cd demo 稍等片刻后,Scr ...
- Angular学习笔记—创建一个angular项目
开始项目前,你需要先安装node和npm,然后执行npm install -g @angular/cli安装Angular CLI. 如何安装node.js和npm npm使用介绍 1.安装angul ...
- NGUI学习笔记(一)UILabel介绍
来个前言: 作为一个U3D程序员,自然要写一写U3D相关的内容了.想来想去还是从UI开始搞起,可能这也是最直观同时也最重要的部分之一了.U3D自带的UI系统,也许略坑,也没有太多介绍的价值,那么从今天 ...
随机推荐
- QVW中实现日期区间的选择功能!
QV在日期的选择上不是很灵活,日期区段的选择可以在列表框中直接用鼠标拖拉区段,如果跨周期比较长了还是不是很方便啦. 下面介绍的方式是完全实现了起始日期的选择功能. 注:日期这个字段在抽取的时候一定要格 ...
- ios开发中MVC模式的理解
MVC是80年代出现的一种软件设计模式,是模型(model),视图(view)和控制(Controller)的缩写. 其中Model的主要功能包括业务逻辑的处理以及数据的访问,这是应用程序的主体部分. ...
- JS常用的7中跨域方式总结
javascript跨域有两种情况: 1.基于同一父域的子域之间,如:a.c.com和b.c.com 2.基于不同的父域之间,如:www.a.com和www.b.com 3.端口的不同,如:ww ...
- HDU1557权利选举
/* 思路:遍历所有2^n个集合,对于每个集合求票和,如果满足票为优胜团体,而再对集合每个成员比较,是否满足变成非优胜团体,是的话,对于该成员对应结果+1. 重点:利用二进制思想,所有团体均对应0~2 ...
- 环状DNA序列
大意: 一个DNA序列是环状的,这意味着有N个碱基的序列有N种表示方法(假设无重复).而这N个序列有一种最小的表示,这个最小表示的意思是这个序列的字典序最小(字典序的意思是在字典中的大小 比如ABC& ...
- linux用户管理,linux用户口令管理,linux用户组管理,linux用户权限管理详解
linux用户管理 http://www.qq210.com/shoutu/android 用户账号的添加(新加用户需添加用户口令) :增加用户账号就是在/etc/passwd文件中为新用户增加一条记 ...
- iOS: 学习笔记, 值与引用类型(译自: https://developer.apple.com/swift/blog/ Aug 15, 2014 Value and Reference Types)
值和引用类型 Value and Reference Types 在Swift中,有两种数据类型. 一是"值类型"(value type), 它是每一个实例都保存有各自的数据,通常 ...
- 关于微软RDLC报表打印时文字拉伸问题(Windows server 2003 sp2)
最近我们开发的打印服务频频出现打印文字拉伸问题,客户意见络绎不绝,最为明显的是使用黑体加粗后 “2.0份” 打印出来后小数点几乎看不见了,用户很容易误认为 “ 20份” .所以问题达到了不得不停下手上 ...
- 上传XML文件字符编码问题
1.上传的XML文件的空格的字符编码和倒入到数据库的空格的字符编码不是一种编码格式,导致导入到数据库的数据和XML文件的数据不一致的情况,进而使展示到界面上的数据在进行搜索时不能搜索出来.解决办法: ...
- VS2012编译生成XP可以执行的程序
首先需要的就是下载VS2012的Update 4更新包,然后打开项目的属性页,在 配置属性->平台工具集 选项中选择 Visual Studio 2012 - Windows XP (v110_ ...