位移:
///
/// 位移拖拽接口
///
public class PosDragAxis : IDragAxis
{
Vector3 pos;
public void DragAxis(GameObject go, Axis a, Vector3 mousePos, Vector3 GOScPos,
Vector3 offset)
{
//获取鼠标的世界坐标
pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
Input.mousePosition.y, GOScPos.z))+offset ;
if (a == Axis.X)
{
//只变换x
go.transform.position= new Vector3(pos.x, go.transform.position.y, go.transform.position.z);
}
else if(a == Axis.Y)
{
//只变换y
go.transform.position = new Vector3(go.transform.position.x, pos.y, go.transform.position.z);
}
else
{
//只变换z
go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y, pos.z);
}
}
}
//旋转
///
/// 位移拖拽接口
///
public class RotateDragAxis : IDragAxis
{
//旋转速度
public float speed = .5f;
//旋转的角度
float ang;
//旋转的上一个向量
public Vector3 lastVector;
//旋转的现在向量
private Vector3 newPos;
//摄像机到物体的向量和物体自身的方向的角度
private float an;
//设置旋转初始向量
public void SetLastPos(Vector3 pos)
{
lastVector = pos;
}
public void DragAxis(GameObject go, Axis a, Vector3 mousePos, Vector3 GOScPos,
Vector3 offset)
{
// var local= go.transform.InverseTransformPoint(pos);
var newVector = Input.mousePosition - GOScPos;
//叉乘获取角度正负
Vector3 cross= Vector3.Cross(lastVector, newVector);
float ang = Vector3.Angle(lastVector, newVector);
ang = cross.z > 0 ? -ang : ang;
if (a == Axis.X)
{
//获取摄像机到物体的向量和物体自身的方向的角度
an = Vector3.Angle(go.transform.position - Camera.main.transform.position, go.transform.right);
//根据角度判断旋转角度的正负
if (an < 90f || an > 270f) ang = -ang;
go.transform.Rotate(Vector3.right, ang * speed, Space.Self);
}
else if (a == Axis.Y)
{
an = Vector3.Angle(go.transform.position-Camera.main.transform.position, go.transform.up);
if (an < 90f || an > 270f) ang = -ang;
go.transform.Rotate(Vector3.up, ang * speed, Space.Self);
}
else
{
an = Vector3.Angle(go.transform.position - Camera.main.transform.position, go.transform.forward);
if (an < 90f || an > 270f) ang = -ang;
go.transform.Rotate(Vector3.forward, ang * speed, Space.Self);
}
//必要一步,过渡
lastVector = newVector;
}
}
//缩放
///
/// 缩放拖拽接口
///
public class ScaleDragAxis : IDragAxis
{
//增量
float delet;
//上一级位置 自身坐标
public Vector3 lastPos;
//鼠标相对聚焦物体的坐标
Vector3 pos;
//设置初始位置
public void SetLastPos(Vector3 pos)
{
lastPos = pos;
}
public void DragAxis(GameObject go, Axis a, Vector3 mousePos, Vector3 GOScPos,
Vector3 offset)
{
//获取鼠标相对聚焦物体的坐标
pos = go.transform.InverseTransformDirection( Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,
Input.mousePosition.y, GOScPos.z)) + offset);
if (a == Axis.X)
{
//获得x轴的增量
delet = pos.x - lastPos.x;
//赋值
go.transform.localScale = new Vector3(go.transform.localScale.x+ delet,
go.transform.localScale.y, go.transform.localScale.z);
}
else if (a == Axis.Y)
{
delet =pos.y - lastPos.y;
go.transform.localScale = new Vector3(go.transform.localScale.x ,
go.transform.localScale.y + delet , go.transform.localScale.z);
}
else
{
delet = pos.z - lastPos.z;
go.transform.localScale = new Vector3(go.transform.localScale.x ,
go.transform.localScale.y, go.transform.localScale.z + delet );
}
//过渡
lastPos = pos;
}
}
最后发给大家一个我自做的插件(专门用来在运行状态下编辑物体空间属性的)
链接:https://pan.baidu.com/s/1V4JOeY1HlVaL1GxvHvUDZA 密码:lkbe