用NGUI写了个虚拟按钮,能用,但很卡,求救啊!
本帖最后由 luckytjm 于 2013-9-11 23:40 编辑先说一下具体情况,然后再上代码吧,首先呢,按钮是NGUI做的,然后把原先Box Collider换成Sphere Collider,建了一个新的背景图文集,最后再添加了我写的这串代码就没了,请各路大神帮忙研究一下哈,到底卡在哪个地方了哈。对了,还有就是我的分辨率是517*291哈,下面是上代码:using UnityEngine;
using System.Collections;
public class JoyControl : MonoBehaviour
{
public GameObject playerTank;
private float joyInitX; //note 摇杆的圆心初始坐标X note
private float joyInitY; //note 摇杆的圆心初始坐标Y note
private float joyMoveDistance; //note 摇杆最大可移动的距离 note
private float currentDistance; //note 单签摇杆圆心离初始圆心的距离 note
private float joyControlDistance; //note 摇杆开始控制坦克移动的最小距离 note
private float mousePositionX; //note 鼠标相对NGUI坐标系的坐标X note
private float mousePositionY; //note 鼠标相对NGUI坐标系的坐标X note
private float parallelMoveLeftX; //note 摇杆控制坦克平行移动左边界X note
private float parallelMoveRightX; //note 摇杆控制坦克平行移动右边界X note
private float parallelMoveLeftY; //note 摇杆控制坦克平行移动左边界Y note
private float parallelMoveRightY; //note 摇杆控制坦克平行移动右边界X note
void Start ()
{
joyInitX = this.transform.localPosition.x;
joyInitY = this.transform.localPosition.y;
joyMoveDistance = 22.5f;
joyControlDistance = 5.0f;
mousePositionX = this.transform.localPosition.x;
mousePositionY = this.transform.localPosition.y;
parallelMoveLeftX = this.transform.localPosition.x - 5.0f;
parallelMoveRightX = this.transform.localPosition.x + 5.0f;
parallelMoveLeftY = this.transform.localPosition.y - 5.0f;
parallelMoveRightY = this.transform.localPosition.y + 5.0f;
}
void OnDrag (Vector2 delta)
{
if(GameObject.Find("Main Camera").GetComponent<MyCamera>().cameraState == 1)
{
// note 获得单签摇杆圆心离初始圆心的距离 note
currentDistance = Mathf.Sqrt(getAbs(this.transform.localPosition.x, joyInitX) * getAbs(this.transform.localPosition.x, joyInitX)
+ getAbs(this.transform.localPosition.y, joyInitY) * getAbs(this.transform.localPosition.y, joyInitY));
if(currentDistance < joyMoveDistance)
{
//note 摇杆随鼠标移动 note
mousePositionX = Input.mousePosition.x - (float)Screen.width / 2;
mousePositionY = Input.mousePosition.y - (float)Screen.height / 2;
this.transform.localPosition = new Vector3(mousePositionX, mousePositionY, 0);
//note 判断摇杆移动距离是否符合控制坦克 note
if(joyControlDistance < currentDistance)
{
//note 判断坦克是否在前后平行活动距离内 note
if(this.transform.localPosition.x > parallelMoveLeftX && this.transform.localPosition.x < parallelMoveRightX)
{
// note 坦克移动 note
if(this.transform.localPosition.y > joyInitY)
{
playerTank.transform.Translate(Vector3.forward * Time.deltaTime * getAbs(this.transform.localPosition.y, joyInitY) * GameObject.Find("PlayerTank").GetComponent<Tank>().tankMoveSpeed);
}
else
{
playerTank.transform.Translate(Vector3.back * Time.deltaTime * getAbs(this.transform.localPosition.y, joyInitY) * GameObject.Find("PlayerTank").GetComponent<Tank>().tankMoveSpeed);
}
}
else if(this.transform.localPosition.y < parallelMoveLeftY && this.transform.localPosition.y > parallelMoveRightY)
{
//note 横向拖动摇杆不做任何操作 note
}
else
{
//note 坦克移动和旋转 note
if(this.transform.localPosition.y > joyInitY)
{
playerTank.transform.Translate(Vector3.forward * Time.deltaTime * getAbs(this.transform.localPosition.y, joyInitY) * GameObject.Find("PlayerTank").GetComponent<Tank>().tankMoveSpeed);
if(this.transform.localPosition.x < joyInitX)
{
playerTank.transform.Rotate(Vector3.down * Time.deltaTime * getAbs(this.transform.localPosition.x, joyInitX) * GameObject.Find("PlayerTank").GetComponent<Tank>().tankRotateSpeed);
}
else
{
playerTank.transform.Rotate(Vector3.up * Time.deltaTime * getAbs(this.transform.localPosition.x, joyInitX) * GameObject.Find("PlayerTank").GetComponent<Tank>().tankRotateSpeed);
}
}
else
{
playerTank.transform.Translate(Vector3.back * Time.deltaTime * getAbs(this.transform.localPosition.y, joyInitY) * GameObject.Find("PlayerTank").GetComponent<Tank>().tankMoveSpeed);
if(this.transform.localPosition.x < joyInitX)
{
playerTank.transform.Rotate(Vector3.up * Time.deltaTime * getAbs(this.transform.localPosition.x, joyInitX) * GameObject.Find("PlayerTank").GetComponent<Tank>().tankRotateSpeed);
}
else
{
playerTank.transform.Rotate(Vector3.down * Time.deltaTime * getAbs(this.transform.localPosition.x, joyInitX) * GameObject.Find("PlayerTank").GetComponent<Tank>().tankRotateSpeed);
}
}
}
}
}
}
else if(GameObject.Find("Main Camera").GetComponent<MyCamera>().cameraState == 2)
{
}
}
/**
* note 计算两个值的绝对值之差的绝对值 note
*/
float getAbs(float a, float b)
{
return Mathf.Abs(Mathf.Abs(a)-Mathf.Abs(b));
}
}
很不错 楼主是超人 好帖就是要顶 说的非常好 不错不错 很不错 好帖就是要顶 难得一见的好帖 很好哦