这是第一个 Unity 开放项目的repo,是 Unity 和社区合作创建的一个小型开源游戏演示,第一款游戏是一款名为 Chop Chop 的动作冒险游戏。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

45 行
1.0 KiB

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
[ExecuteInEditMode]
[AddComponentMenu("UOP1/Tools/Click to Place")]
public class ClickToPlaceHelper : MonoBehaviour
{
[Tooltip("Vertical offset above the clicked point. Useful to avoid spawn points to be directly ON the geometry which might cause issues.")]
[SerializeField] private float _verticalOffset = 0.1f;
private Vector3 _targetPosition;
public bool IsTargeting { get; private set; }
private void OnDrawGizmos()
{
if (IsTargeting)
{
Gizmos.color = Color.green;
Gizmos.DrawCube(_targetPosition, Vector3.one * 0.3f);
}
}
public void BeginTargeting()
{
IsTargeting = true;
_targetPosition = transform.position;
}
public void UpdateTargeting(Vector3 spawnPosition)
{
_targetPosition = spawnPosition + Vector3.up * _verticalOffset;
}
public void EndTargeting()
{
IsTargeting = false;
#if UNITY_EDITOR
Undo.RecordObject(transform, $"{gameObject.name} moved by ClickToPlaceHelper");
#endif
transform.position = _targetPosition;
}
}