diff --git a/CharacterPreview/CharacterPreview.csproj b/CharacterPreview/CharacterPreview.csproj index 39e50cc..779a740 100644 --- a/CharacterPreview/CharacterPreview.csproj +++ b/CharacterPreview/CharacterPreview.csproj @@ -22,11 +22,12 @@ - - ..\..\..\steam\steamapps\common\Escape from Duckov\Duckov_Data\Managed\Eflatun.SceneReference.dll - + + + + diff --git a/CharacterPreview/Config.cs b/CharacterPreview/Config.cs new file mode 100644 index 0000000..07098eb --- /dev/null +++ b/CharacterPreview/Config.cs @@ -0,0 +1,64 @@ +using System; +using UnityEngine; +using System.IO; + +namespace CharacterPreview +{ + [Serializable] + public class ConfigData + { + public Vector3 modelPosition; + public Vector3 modelRotation; + public float modelScale; + public bool use = false; + public bool tip = true; + public bool hideCamera = true; + public bool showSetEditButton = true; + public bool canEdit = true; + public float editSpeed = 1; + public bool showEquipment = true; + } + + [Serializable] + public class Config + { + private string configSavePath; + + public ConfigData data = new ConfigData(); + + public Config(string savePath) + { + configSavePath = savePath; + } + + public void Save() + { + data.use = true; + var json = JsonUtility.ToJson(data, true); + File.WriteAllText(configSavePath, json); + } + + public static Config Load(string savePath) + { + if (!File.Exists(savePath)) + { + try + { + File.Create(savePath).Dispose(); + } + catch (IOException ex) + { + Debug.LogError($"Failed to create empty config file at {savePath}: {ex.Message}"); + return new Config(savePath) { data = new ConfigData() }; + } + } + + var json = File.ReadAllText(savePath); + var loadedData = JsonUtility.FromJson(json); + + var config = new Config(savePath); + config.data = loadedData ?? new ConfigData(); + return config; + } + } +} \ No newline at end of file diff --git a/CharacterPreview/ControlModelMove.cs b/CharacterPreview/ControlModelMove.cs new file mode 100644 index 0000000..7fac508 --- /dev/null +++ b/CharacterPreview/ControlModelMove.cs @@ -0,0 +1,265 @@ +using System; +using TMPro; +using Unity.VisualScripting; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace CharacterPreview +{ + public class ControlModelMove : MonoBehaviour, IDragHandler,IPointerDownHandler,IPointerEnterHandler,IPointerExitHandler + { + private RectTransform rectTransform; + private Transform canvasRectTransform; //由滑动器自己创建的 + private Vector2 lastMousePosition; + private Image image; + private TMP_Text text; + private Button editButton; + private TMP_Text editButtonText; + + public bool firstClick=true; + + public float Speed => ModBehaviour.config.data.editSpeed; + //防止鼠标在范围外捕获消息 + private bool canEdit = false; + + private void Awake() + { + SetRectTransform(); + SetText(); + SetColor(); + if(ModBehaviour.config.data.showSetEditButton) + SetEditButton(); + firstClick = ModBehaviour.config.data.tip; + if (!firstClick) + { + HideTip(); + } + } + + private void OnDestroy() + { + if (canvasRectTransform) + { + Destroy(canvasRectTransform.gameObject); + } + } + + void Update() + { + if (!canEdit) + return; + var scroll = Input.GetAxis("Mouse ScrollWheel"); + if (scroll != 0) + { + if (Input.GetMouseButton(1)) + { + ModBehaviour.modelMove.RotateAroundCameraZ(Speed * scroll * 8); + } + else if(Input.GetKey(KeyCode.LeftShift)) + { + ModBehaviour.modelMove.Scale(Speed*scroll/4f); + } + else + { + ModBehaviour.modelMove.Move(new Vector3(0, 0, Speed * scroll * 2)); + } + } + + if (Input.GetMouseButtonDown(2)) + { + if (Input.GetKey(KeyCode.LeftControl)) + { + ModBehaviour.modelMove.RefreshPosition(); + } + else + { + ModBehaviour.modelMove.LookAtCamera(); + } + } + } + + public void SetColor() + { + var color = new Color(0.9f, 0.8f, 0.3f, 0.1f); + image = gameObject.GetComponent(); + if (!image) + { + image = gameObject.AddComponent(); + } + + image.color = color; + } + + public void SetText() + { + text=gameObject.GetComponentInChildren(); + if (!text) + { + var textChilde = new GameObject("Text"); + textChilde.transform.SetParent(gameObject.transform); + var rect = textChilde.AddComponent(); + text= textChilde.AddComponent(); + + rect.anchorMax = Vector2.one; + rect.anchorMin = Vector2.zero; + rect.offsetMin = Vector2.zero; + rect.offsetMax = Vector2.zero; + } + text.fontSize = 24; + text.alignment=TextAlignmentOptions.Center; + text.text = "在此区域可以编辑模型状态(点击区域关闭提示)\n" + + "通过鼠标左键拖动修改角色的上下左右位置\n" + + "通过鼠标滚轮修改角色的z轴位置\n" + + "通过鼠标右键控制角色旋转\n" + + "按住右键的情况下滚动滚轮可让角色转圈\n" + + "按住shift滚动滚轮可缩放角色\n" + + "点按鼠标中键可让角色朝向摄像头\n" + + "按住ctrl并点击中键则恢复默认位置"; + } + + public void SetRectTransform() + { + if (!rectTransform) + { + rectTransform = GetComponent(); + } + + if (!rectTransform) + { + rectTransform = gameObject.AddComponent(); + } + + if (!rectTransform.parent || rectTransform.parent.name != "Canvas") + { + var defaultCanvas = GameObject.Find("Canvas"); + if (!defaultCanvas) + { + defaultCanvas = new GameObject("ControlModelMoveCanvas"); + var canvas = defaultCanvas.AddComponent(); + canvas.renderMode = RenderMode.ScreenSpaceOverlay; // 设置渲染模式为屏幕空间覆盖 + defaultCanvas.AddComponent(); + defaultCanvas.AddComponent(); + canvasRectTransform = defaultCanvas.transform; + } + + rectTransform.SetParent(defaultCanvas.transform); + } + + rectTransform.SetAsFirstSibling(); + + rectTransform.anchorMax = Vector2.one; + rectTransform.anchorMin = new Vector2(0.5f, 0); + rectTransform.offsetMax = Vector2.zero; + rectTransform.offsetMin = Vector2.zero; + } + + public void SetEditButton() + { + if (!editButton) + { + var buttonObj = new GameObject("EditButton"); + buttonObj.transform.SetParent(transform, false); + var buttonRect= buttonObj.AddComponent(); + buttonRect.anchorMax=Vector2.right; + buttonRect.anchorMin=Vector2.right; + buttonRect.pivot = Vector2.right; + buttonRect.sizeDelta=new Vector2(80f,30f); + buttonRect.anchoredPosition = new Vector2(-200, 100); + + + var button = buttonObj.AddComponent