195 lines
4.6 KiB
C#
195 lines
4.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Blueprint : MonoBehaviour
|
|
{
|
|
public GameObject Prefab_Realization;
|
|
|
|
public void Init()
|
|
{
|
|
GameObject examp = Instantiate(Prefab_Realization);
|
|
gameObject.SetMeshRendererActive(false);
|
|
examp.CopyTransform(gameObject);
|
|
initParent = transform.parent;
|
|
}
|
|
|
|
public bool FixedPosX, FixedPosY, FixedPosZ, FixedRotX, FixedRotY, FixedRotZ;
|
|
|
|
public Vector3 fixPos, fixRot;
|
|
|
|
/// <summary>
|
|
/// 固定位置
|
|
/// </summary>
|
|
public bool FixedLocation;
|
|
private Vector3 fixedPos;
|
|
/// <summary>
|
|
/// 结束编辑
|
|
/// </summary>
|
|
public bool EndEditor;
|
|
private bool mouseMoved;
|
|
|
|
/// <summary>
|
|
/// 初始化的父物体
|
|
/// </summary>
|
|
private Transform initParent;
|
|
|
|
/// <summary>
|
|
/// 场景触发器
|
|
/// </summary>
|
|
public string SceneColliderName;
|
|
private bool colliderStay;
|
|
private Transform colliderTransform;
|
|
private Optional colliderOptional;
|
|
|
|
private void Update()
|
|
{
|
|
|
|
if (!FixedLocation)
|
|
{
|
|
ConfirmPosition();
|
|
}
|
|
else
|
|
{
|
|
ConfirmRotation();
|
|
}
|
|
|
|
if (FixedLocation)
|
|
transform.position = fixedPos;
|
|
else
|
|
{
|
|
Vector3 temp = transform.position;
|
|
if (FixedPosX)
|
|
{
|
|
temp.x = fixPos.x;
|
|
}
|
|
if (FixedPosY)
|
|
{
|
|
temp.y = fixPos.y;
|
|
}
|
|
if (FixedPosZ)
|
|
{
|
|
temp.z = fixPos.z;
|
|
}
|
|
transform.position = temp;
|
|
}
|
|
|
|
if (EndEditor)
|
|
{ }
|
|
else
|
|
{
|
|
Vector3 temp = transform.eulerAngles;
|
|
if (FixedRotX)
|
|
{
|
|
temp.x = fixRot.x;
|
|
}
|
|
if (FixedRotY)
|
|
{
|
|
temp.y = fixRot.y;
|
|
}
|
|
if (FixedRotZ)
|
|
{
|
|
temp.z = fixRot.z;
|
|
}
|
|
transform.eulerAngles = temp;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.name.Equals(SceneColliderName))
|
|
{
|
|
colliderTransform = other.transform;
|
|
colliderOptional = other.GetComponentInParent<Optional>();
|
|
colliderStay = true;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.name.Equals(SceneColliderName))
|
|
{
|
|
colliderTransform = null;
|
|
colliderOptional = null;
|
|
colliderStay = false;
|
|
}
|
|
}
|
|
|
|
private void OnMouseEnter()
|
|
{
|
|
if (!EndEditor) return;
|
|
if (colliderOptional == null) colliderOptional = colliderTransform.GetComponentInParent<Optional>();
|
|
if (colliderOptional == null) return;
|
|
colliderOptional.MouseEnter();
|
|
}
|
|
|
|
private void OnMouseDown()
|
|
{
|
|
if (!EndEditor) return;
|
|
if (colliderOptional == null) return;
|
|
colliderOptional.MouseDown();
|
|
}
|
|
|
|
private void OnMouseExit()
|
|
{
|
|
if (!EndEditor) return;
|
|
if (colliderOptional == null) return;
|
|
colliderOptional.MouseExit();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 确认位置
|
|
/// </summary>
|
|
public void ConfirmPosition()
|
|
{
|
|
if (!string.IsNullOrEmpty(SceneColliderName) && !colliderStay) return;
|
|
if (Input.GetMouseButtonUp(0))
|
|
{
|
|
fixedPos = transform.position;
|
|
FixedLocation = true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 确认角度
|
|
/// </summary>
|
|
public void ConfirmRotation()
|
|
{
|
|
if (!EndEditor)
|
|
{
|
|
if (Input.GetMouseButtonDown(0))
|
|
{
|
|
mouseMoved = false;
|
|
}
|
|
//确定旋转角度
|
|
if (Input.GetMouseButtonUp(0) && !mouseMoved)
|
|
{
|
|
EndEditor = true;
|
|
transform.parent = colliderTransform.parent;
|
|
colliderTransform.GetComponentInParent<Optional>().SolveIssue(this.gameObject);
|
|
}
|
|
//更改旋转角度
|
|
if (Input.GetMouseButton(0))
|
|
{
|
|
var mx = Input.GetAxis("Mouse X");
|
|
if (mx != 0) mouseMoved = true;
|
|
Vector3 eulerang = transform.eulerAngles;
|
|
eulerang.y += mx;
|
|
transform.eulerAngles = eulerang;
|
|
}
|
|
|
|
CancelConfirmation();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取消确认
|
|
/// </summary>
|
|
public void CancelConfirmation()
|
|
{
|
|
//移动或旋转角色时退出固定位置
|
|
if (Input.GetMouseButtonDown(1) || Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
|
|
FixedLocation = false;
|
|
}
|
|
}
|