DragPointProp.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Rendering;
  5. namespace MeidoPhotoStudio.Plugin;
  6. public class DragPointProp : DragPointGeneral
  7. {
  8. public string AssetName = string.Empty;
  9. private List<Renderer> renderers;
  10. public AttachPointInfo AttachPointInfo { get; private set; } = AttachPointInfo.Empty;
  11. public PropInfo Info { get; set; }
  12. public string Name =>
  13. MyGameObject.name;
  14. public bool ShadowCasting
  15. {
  16. get => renderers.Count is not 0 && renderers.Any(r => r.shadowCastingMode is ShadowCastingMode.On);
  17. set
  18. {
  19. foreach (var renderer in renderers)
  20. renderer.shadowCastingMode = value ? ShadowCastingMode.On : ShadowCastingMode.Off;
  21. }
  22. }
  23. public override void Set(Transform myObject)
  24. {
  25. base.Set(myObject);
  26. DefaultRotation = MyObject.rotation;
  27. DefaultPosition = MyObject.position;
  28. DefaultScale = MyObject.localScale;
  29. renderers = new(MyObject.GetComponentsInChildren<Renderer>());
  30. }
  31. public void AttachTo(Meido meido, AttachPoint point, bool keepWorldPosition = true)
  32. {
  33. var attachPoint = meido?.IKManager.GetAttachPointTransform(point);
  34. AttachPointInfo = meido is null ? AttachPointInfo.Empty : new(point, meido);
  35. // TODO: Use transform.SetPositionAndRotation MyObject.position = position;
  36. var position = MyObject.position;
  37. var rotation = MyObject.rotation;
  38. var scale = MyObject.localScale;
  39. MyObject.transform.SetParent(attachPoint, keepWorldPosition);
  40. if (keepWorldPosition)
  41. {
  42. MyObject.rotation = rotation;
  43. }
  44. else
  45. {
  46. MyObject.localPosition = Vector3.zero;
  47. MyObject.rotation = Quaternion.identity;
  48. }
  49. MyObject.localScale = scale;
  50. if (!attachPoint)
  51. Utility.FixGameObjectScale(MyGameObject);
  52. }
  53. public void DetachFrom(bool keepWorldPosition = true) =>
  54. AttachTo(null, AttachPoint.None, keepWorldPosition);
  55. public void DetachTemporary()
  56. {
  57. MyObject.transform.SetParent(null, true);
  58. Utility.FixGameObjectScale(MyGameObject);
  59. }
  60. protected override void ApplyDragType()
  61. {
  62. var active = DragPointEnabled && Transforming || Special;
  63. ApplyProperties(active, active, GizmoEnabled && Rotating);
  64. ApplyColours();
  65. }
  66. protected override void OnDestroy()
  67. {
  68. Destroy(MyGameObject);
  69. base.OnDestroy();
  70. }
  71. }