Created
January 19, 2016 04:54
-
-
Save seldomU/170de2c4634fcae5f3bf to your computer and use it in GitHub Desktop.
RelationsInspector test backend, with toolbar and search
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 TestBackend3 : 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() ) ); | |
} | |
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 ); | |
{ | |
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