Last active
August 29, 2015 14:06
-
-
Save joonjoonjoon/2f9b73c5b20010fb42f2 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 System; | |
| using Holoville.HOTween.Path; | |
| using UnityEditor; | |
| using UnityEngine; | |
| [CustomEditor(typeof (SomeMonobehavior))] | |
| public class SomeMonobehaviorEditor : Editor | |
| { | |
| public override void OnInspectorGUI() | |
| { | |
| DrawDefaultInspector(); | |
| if (GUILayout.Button("remove and add component")) | |
| { | |
| var gameObject = ((SomeMonobehavior) target).gameObject; | |
| try | |
| { | |
| // Illustrating with BoxCollider as example | |
| var old = gameObject.GetComponent<BoxCollider>(); | |
| if (old != null) | |
| { | |
| DestroyImmediate(old); | |
| } | |
| } | |
| catch (NullReferenceException) | |
| { | |
| // this occurs properly when the object does not exist (though the != null is catching it now) | |
| } | |
| catch (MissingReferenceException) | |
| { | |
| // the code never enters here, even though the exception is of this type. | |
| } | |
| // add it again | |
| var path = gameObject.AddComponent<BoxCollider>(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment