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; /// /// 固定位置 /// public bool FixedLocation; private Vector3 fixedPos; /// /// 结束编辑 /// public bool EndEditor; private bool mouseMoved; /// /// 初始化的父物体 /// private Transform initParent; /// /// 场景触发器 /// 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(); 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(); 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(); } /// /// 确认位置 /// public void ConfirmPosition() { if (!string.IsNullOrEmpty(SceneColliderName) && !colliderStay) return; if (Input.GetMouseButtonUp(0)) { fixedPos = transform.position; FixedLocation = true; } } /// /// 确认角度 /// public void ConfirmRotation() { if (!EndEditor) { if (Input.GetMouseButtonDown(0)) { mouseMoved = false; } //确定旋转角度 if (Input.GetMouseButtonUp(0) && !mouseMoved) { EndEditor = true; transform.parent = colliderTransform.parent; colliderTransform.GetComponentInParent().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(); } } /// /// 取消确认 /// public void CancelConfirmation() { //移动或旋转角色时退出固定位置 if (Input.GetMouseButtonDown(1) || Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) FixedLocation = false; } }