TeleportURPHelper.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Valve.VR.InteractionSystem
  5. {
  6. [ExecuteInEditMode]
  7. public class TeleportURPHelper : MonoBehaviour
  8. {
  9. #if UNITY_URP && UNITY_EDITOR
  10. void Start()
  11. {
  12. if (UnityEditor.PrefabUtility.IsPartOfPrefabInstance(this) == false)
  13. return;
  14. string teleportAssetPath = UnityEditor.PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(this);
  15. GameObject teleportPrefab = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(teleportAssetPath);
  16. Teleport teleport = teleportPrefab.GetComponent<Teleport>();
  17. UnityEditor.SerializedObject serializedTeleport = new UnityEditor.SerializedObject(teleport);
  18. serializedTeleport.Update();
  19. bool changed = false;
  20. changed |= FindURPVersion(serializedTeleport, "areaHighlightedMaterial");
  21. changed |= FindURPVersion(serializedTeleport, "areaLockedMaterial");
  22. changed |= FindURPVersion(serializedTeleport, "areaVisibleMaterial");
  23. changed |= FindURPVersion(serializedTeleport, "pointHighlightedMaterial");
  24. changed |= FindURPVersion(serializedTeleport, "pointLockedMaterial");
  25. changed |= FindURPVersion(serializedTeleport, "pointVisibleMaterial");
  26. if (changed)
  27. {
  28. serializedTeleport.ApplyModifiedProperties();
  29. UnityEditor.EditorUtility.SetDirty(teleport);
  30. }
  31. TeleportArc arc = teleportPrefab.GetComponent<TeleportArc>();
  32. UnityEditor.SerializedObject serializedArc = new UnityEditor.SerializedObject(arc);
  33. serializedArc.Update();
  34. changed = FindURPVersion(serializedArc, "material");
  35. if (changed)
  36. {
  37. serializedArc.ApplyModifiedProperties();
  38. UnityEditor.EditorUtility.SetDirty(arc);
  39. }
  40. }
  41. private bool FindURPVersion(UnityEditor.SerializedObject teleportObject, string propertyName)
  42. {
  43. UnityEditor.SerializedProperty materialProp = teleportObject.FindProperty(propertyName);
  44. Material original = materialProp.objectReferenceValue as Material;
  45. if (original != null && !original.name.StartsWith("URP"))
  46. {
  47. string[] mats = UnityEditor.AssetDatabase.FindAssets(string.Format("URP{0} t:material", original.name));
  48. if (mats.Length > 0)
  49. {
  50. string path = UnityEditor.AssetDatabase.GUIDToAssetPath(mats[0]);
  51. materialProp.objectReferenceValue = UnityEditor.AssetDatabase.LoadAssetAtPath<Material>(path);
  52. return true;
  53. }
  54. }
  55. return false;
  56. }
  57. #endif
  58. }
  59. }