Created
January 19, 2016 04:55
-
-
Save seldomU/c7f3caa78d10d6b3491a to your computer and use it in GitHub Desktop.
RelationsInspector test backend, with adding and removing GameObjects
This file contains 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 UnityEngine; | |
using UnityEditor; | |
using System.Collections.Generic; | |
using System.Linq; | |
using RelationsInspector; | |
using RelationsInspector.Backend; | |
public class TestBackend4 : MinimalBackend<GameObject, string> | |
{ | |
string searchstring = string.Empty; | |
public override IEnumerable<Relation<GameObject, string>> GetRelations( GameObject entity ) | |
{ | |
// parent -> entity | |
if ( entity.transform.parent != null ) | |
yield return new Relation<GameObject, string>( entity.transform.parent.gameObject, entity, string.Empty ); | |
// entity -> children | |
foreach ( Transform t in entity.transform ) | |
yield return new Relation<GameObject, string>( entity, t.gameObject, string.Empty ); | |
} | |
public override void OnEntityContextClick( IEnumerable<GameObject> entities, GenericMenu menu ) | |
{ | |
menu.AddItem( new GUIContent( "Add as child" ), false, () => api.InitRelation( entities.ToArray() ) ); | |
menu.AddItem( new GUIContent( "Delete entity" ), false, () => { foreach ( var e in entities ) DeleteEntity( e ); } ); | |
} | |
void DeleteEntity( GameObject entity ) | |
{ | |
api.RemoveEntity( entity ); | |
Undo.DestroyObjectImmediate( entity ); | |
} | |
public override void CreateRelation( GameObject source, GameObject target) | |
{ | |
Undo.SetTransformParent( target.transform, source.transform, "Adding as child" ); | |
EditorUtility.SetDirty( source ); | |
EditorUtility.SetDirty( target ); | |
api.AddRelation( source, target, string.Empty ); | |
} | |
public override void OnRelationContextClick( Relation<GameObject, string> relation, GenericMenu menu ) | |
{ | |
menu.AddItem( new GUIContent( "Un-child" ), false, () => DeleteRelation( relation.Source, relation.Target, relation.Tag ) ); | |
} | |
void DeleteRelation( GameObject source, GameObject target, string tag ) | |
{ | |
Undo.SetTransformParent( target.transform, null, "Un-childing" ); | |
EditorUtility.SetDirty( source ); | |
EditorUtility.SetDirty( target ); | |
api.RemoveRelation( source, target, tag ); | |
} | |
public override Rect OnGUI() | |
{ | |
GUILayout.BeginHorizontal( EditorStyles.toolbar ); | |
{ | |
if ( GUILayout.Button( "Add GameObject", EditorStyles.toolbarButton, GUILayout.ExpandWidth( false ) ) ) | |
{ | |
var newGO = new GameObject(); | |
Undo.RegisterCreatedObjectUndo( newGO, "Created GameObject" ); | |
api.AddEntity( newGO, Vector2.zero ); | |
} | |
GUILayout.FlexibleSpace(); | |
searchstring = BackendUtil.DrawEntitySelectSearchField( searchstring, api ); | |
} | |
GUILayout.EndHorizontal(); | |
return base.OnGUI(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment