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系统,也许略坑,也没有太多介绍的价值,那么从今天 ...
随机推荐
- (转)C#中Trim()、TrimStart()、TrimEnd()的用法 .
C#中Trim().TrimStart().TrimEnd()的用法: 这三个方法用于删除字符串头尾出现的某些字符.Trim()删除字符串头部及尾部出现的空格,删除的过程为从外到内,直到碰到一个非空格 ...
- 微信热修复tinker及tinker server快速接入
博客: 安卓之家 掘金: jp1017 微博: 追风917 CSDN: 蒋朋的家 简书: 追风917 当前热修复方案很多,今天研究了下微信的tinker,使用效果还是不错的,配合tinker serv ...
- 对于百川SDK签名验证的问题
SDK是要在wantu.taobao.com生成的.而生成这个SDK其实是要上传一个apk,而这个上传其实就是取他的签名而已.验证就是那张yw222那张图片.重点是你上传的apk的签名是不是跟你的生成 ...
- the account is currently locked out. The system administrator can unlock it.
今天遇到的问题比较有意思.首先是很久没有打开测试数据库了,今天打开,使用service程序测试的时候出现下面的错误提示:Message: System.Data.SqlClient.SqlExcept ...
- iOS 正则表达式-判断邮箱、手机号
判断是否是邮箱 -(BOOL)isValidateEmail:(NSString *)email { NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[ ...
- Linux 防火墙设置,禁止某个ip访问
service iptables status 查看防火墙状态 service iptables start 开启防火墙 service iptables ...
- js简版定时器
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- Yeoman安装
Yeoman帮助我们创建项目,提供更好的工具来使我们的项目更多样化. Yeoman提供generator系统,一个generator是一个插件,在我们在一个完整的项目上使用‘yo’命令时,会运行该ge ...
- Android PackageManager packages.xml文件格式
packages.xml文件存放在/data/system目录下 该文件记录了系统中所有应用程序的包管理相关信息 PmS根据该文件进行包管理的各种操作 标签名称 所包含的值举例 last- ...
- 用Django搭建个人博客—(3)
今日主题 定义博客文章和评论的的数据库定义 定义操作这几个Model的后台数据 User表 USER_STATUS = ( ('active', u'激活'), ('suspended', u'禁用' ...