Created
October 12, 2018 14:45
-
-
Save dialupnoises/c407614b86dda68a01094624fb2012eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Sirenix.OdinInspector; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
#endif | |
/// <summary> | |
/// Placed where an obstacle will be spawned when the game loads. | |
/// Used to fake nested prefabs in a pre-2018.3 world. | |
/// </summary> | |
[ExecuteInEditMode] | |
[SelectionBase] | |
public class ObstaclePlaceholder : MonoBehaviour | |
{ | |
/// <summary> | |
/// The obstacle that will be used for this placeholder. | |
/// </summary> | |
[ValueDropdown("DropdownValuesFunction")] | |
public GameObject Obstacle; | |
/// <summary> | |
/// Odin Inspector doesn't let us use ValueDropdown on the same field as | |
/// InlineEditor... so we use this workaround so we can still show the preview | |
/// without sacrificing the dropdown box. | |
/// </summary> | |
[InlineEditor(InlineEditorModes.LargePreview)] | |
[ShowInInspector] | |
[HideLabel] | |
public GameObject ObstacleForPreview => Obstacle; | |
private GameObject previewObject; | |
private GameObject previewSource; | |
/// <summary> | |
/// Passes through DropdownValuesFunction from ObstacleTypes | |
/// for use with OdinInspector. | |
/// </summary> | |
public ValueDropdownList<GameObject> DropdownValuesFunction() => | |
ObstacleTypes.DropdownValuesFunction(); | |
/// <summary> | |
/// The object that's currently being used for the preview. | |
/// </summary> | |
public GameObject PreviewObject => previewObject; | |
// all this code deals with displaying the preview | |
// so we only need to include it in the editor | |
#if UNITY_EDITOR | |
private void Update() | |
{ | |
// if we're playing, destroy any preview objects if any still exist | |
if(Application.isPlaying) | |
{ | |
if(previewObject != null) | |
{ | |
DestroyImmediate(previewObject); | |
} | |
return; | |
} | |
// if the obstacle changed since last frame, replace the current preview | |
if( | |
Obstacle != null && | |
(previewObject == null || previewSource != Obstacle)) | |
{ | |
// destroy if necessary | |
if(previewObject != null) | |
{ | |
DestroyImmediate(previewObject); | |
} | |
// create preview object | |
// we create an empty object for the parent to apply the hideflags to because HideFlags.HideInHierarchy | |
// also implicitly makes it unselectable, and we want them to be selectable | |
previewObject = new GameObject("Preview Parent"); | |
previewObject.transform.parent = transform; | |
previewObject.transform.localScale = Vector3.one; | |
previewObject.transform.localPosition = Vector3.zero; | |
previewObject.hideFlags = HideFlags.HideAndDontSave; | |
previewSource = Obstacle; | |
var obs = Instantiate(Obstacle, previewObject.transform); | |
obs.transform.localPosition = Vector3.zero; | |
obs.hideFlags = HideFlags.HideInInspector; | |
// apply hideflags to children | |
foreach(var t in obs.GetComponentsInChildren<Transform>()) | |
{ | |
t.gameObject.hideFlags = obs.hideFlags; | |
} | |
} | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment