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系统,也许略坑,也没有太多介绍的价值,那么从今天 ...
随机推荐
- 百度,人人,新浪,腾讯等分享Js代码
<!-- Baidu Button BEGIN --> <div id="bdshare" class=" ...
- 带参数的查询防止SQL注入攻击
把单引号替换成两个单引号,虽然能起到一定的防止SQL注入攻击的作用,但是更为有效的办法是把要拼接的内容做成“参数” SQLCommand支持带参数的查询,也就是说,可以在查询语句中指定参数: 参数的设 ...
- 使用ArrayList对大小写字母的随机打印
从a~z以及A~Z随机生成一个字母并打印:打印全部的字母 package com.liaojianya.chapter1; import java.util.ArrayList; /** * This ...
- hdoj 2054(A==B)
注意考虑以下数据: 123 123.0; 0.123 .123; 00.123 0.123; 代码: #include<iostream>#include<cstdio> ...
- windows注册表的基本使用——示例
上网找好多资料发现一个问题就是太老.例如只有RegCreateKey而没有RegCreateKeyEx用法详解,自己摸索了几个小时终于基本用到的几个函数都试用了一遍. 下面代码已经通过编译测试 // ...
- 实现基于Memcache存储的Session类
自主实现Session功能的类,基于文件方式存储Session数据,测试基本通过,还比较好玩,实际应用没有意义,只不过是学习Session是如何实现的. 使用基于文件的Session存取瓶颈可能都是在 ...
- async: false的应用.
目的: 手机webview中, js ajax请求, success后, 进行window.open 操作 问题: 在Android, IOS均不能执行window.open 解决办法: 设置ajax ...
- Struts面试笔记
Struts2面试题1.struts2工作流程Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件. 核心控制器 ...
- ListView的setOnItemClickListener和setOnItemLongClickListener同时响应的问题
lvContentList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(A ...
- 聊一聊c++中指针为空的三种写法 ----->NULL, 0, nullptr
看到同事用了一下nullptr.不是很了解这方面东东,找个帖子学习学习 http://www.cppblog.com/airtrack/archive/2012/09/16/190828.aspx N ...