使用NGUI制作悬浮菜单。
在UIAnchor锚点下的Offset建立背景和按钮菜单。同过InputMouseXY的位置判断。是否应该弹出和收回。
OffSet在此处是作为TweenGameObject的父对象,他被Tween将会带着它下面的子对象一起弹出。这就实现了我们弹出菜单的目的OutPos就是弹出检测的XY值,InPos是收回检测的XY值。实现脚本预览效果如下:
源代码如下:
------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
public class UIGameSetTween : UITweener {
public Vector3 FromPos, ToPos;
//对象是否弹出;
public bool isTweenOut = false;
//弹出对象;
public GameObject tweenGameObject;
//弹出监听点,回收监听点;
public Vector2 outPos, inPos;
override protected void OnUpdate(float factor, bool isFinished)
{
// cachedTransform.localPosition = from * (1f - factor) + to * factor;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Debug.Log(Input.mousePosition);
if (!isTweenOut)
{
//符合弹出监控点;
if(Input.mousePosition.x<=outPos.x&&Input.mousePosition.y<=outPos.y){
ShowInfo(tweenGameObject);
isTweenOut = !isTweenOut;
}
}
if(isTweenOut){
//符合回收监控点;
if(Input.mousePosition.x>=inPos.x || Input.mousePosition.y>=inPos.y){
DisShowInfo(tweenGameObject);
isTweenOut = !isTweenOut;
}
}
}
public void ShowInfo(GameObject twGO) {
TweenPosition tw = UITweener.Begin<TweenPosition>(twGO,duration);
tw.from = FromPos;
tw.to = ToPos;
if(duration<=0){
tw.Sample(0.3f,true);
tw.enabled=false;
}
}
public void DisShowInfo(GameObject twGO)
{
TweenPosition tw = UITweener.Begin<TweenPosition>(twGO, duration);
tw.from = ToPos;
tw.to = FromPos;
if (duration <= 0)
{
tw.Sample(0.3f, true);
tw.enabled = false;
}
}
}