1. 控制球体旋转
if (Input.GetMouseButton(0))
{
rotationX = -Input.GetAxis("Mouse X") * scaleRotationX;
rotationY = Input.GetAxis("Mouse Y") * scaleRotationY;
transform.Rotate(rotationY, rotationX, 0f, Space.World);
}
2. 控制球体移动,放大
if (Input.GetMouseButton(1))
{
positionX = Input.GetAxis("Mouse X") * scalePosiontX;
positionY = Input.GetAxis("Mouse Y") * scalePosiontY;
positionZ = -Input.GetAxis("Mouse ScrollWheel") * scalePosiontZ;
transform.position += new Vector3(positionX, positionY, positionZ);
}
3. 控制地点信息UI展示在地点的上方
showCanvas.transform.localPosition = WorldToUgui(target.position);
public Vector2 WorldToUgui(Vector3 position)
{
Vector2 screenPoint = MainCamera.WorldToScreenPoint(position); //世界坐标转换为屏幕坐标
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
screenPoint -= screenSize / 2; //将屏幕坐标变换为以屏幕中心为原点
Vector2 anchorPos = screenPoint / screenSize * canvasRectTransform.sizeDelta; //缩放得到UGUI坐标
return anchorPos;
}
4. 物体背过球体不展示UI
利用射线来判断点位是否在视线内,
private void CheckCanvas(Transform target, GameObject showCanvas)
{
Physics.SyncTransforms();
RaycastHit hitInfo;
Physics.Raycast(MainCamera.transform.position, (target.position - MainCamera.transform.position).normalized,
out hitInfo);
if (hitInfo.collider == null)
{
return;
}
// Debug.DrawLine(MainCamera.transform.position, (target.position - MainCamera.transform.position).normalized, Color.red);
if (hitInfo.collider.gameObject == gameObject)
{
showCanvas.transform.localScale = Vector3.zero;
}
else
{
showCanvas.transform.localScale = Vector3.one;
showCanvas.transform.localPosition = WorldToUgui(target.position);
}
}
遇到问题:在鼠标快速转动球体的时候射线会在点位物体和球体之前快速抖动,会导致UI闪动的问题,默认情况下,对Unity变换属性的所有改动会在使用前同步到物理引擎。也就是说,在每次运行光线投射前,或每次运行模拟过程前都会进行同步。
同步不是速度最快的操作,它会降低被调用次数。因此,我们加入了新设置,用于禁用所有自动同步点。你应该调用Physics.SyncTransforms,不然Transform只会在模拟过程前,即FixedUpdate前进行同步。
5. 按A||D切换上一个,下一个点位
利用每一帧朝目标向量移动一点的原理来做
IEnumerator RotateFace(Location location)
{
while (true)
{
Vector2 targetDir = FaceCamPostion.position - location.Transform.position;
Debug.Log(targetDir);
if ((targetDir - Vector2.zero).sqrMagnitude < 0.01f)
{
break;
}
transform.Rotate(targetDir.y, -targetDir.x, 0f, Space.World);
yield return new WaitForEndOfFrame();
}
}
6.鼠标在UI上悬停高亮
利用继承系统的Button里面自带的事件来做
public enum ButtonEvent
{
Highlighted,
Normal,
Selected
}
public class ButtonImpl : Button
{
public delegate void MouseEventDelegate(ButtonEvent @event);
public MouseEventDelegate MouseEvent;
protected override void DoStateTransition(SelectionState state, bool instant)
{
base.DoStateTransition(state, instant);
switch (state)
{
case SelectionState.Highlighted:
MouseEvent?.Invoke(ButtonEvent.Highlighted);
break;
case SelectionState.Normal:
MouseEvent?.Invoke(ButtonEvent.Normal);
break;
case SelectionState.Pressed:
break;
}
}
public override void OnPointerClick(PointerEventData eventData)
{
base.OnPointerClick(eventData);
Debug.Log("submit");
MouseEvent?.Invoke(ButtonEvent.Selected);
}
}
打赏