Skip to content

Instantly share code, notes, and snippets.

@seldomU
Created January 19, 2016 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seldomU/c7f3caa78d10d6b3491a to your computer and use it in GitHub Desktop.
Save seldomU/c7f3caa78d10d6b3491a to your computer and use it in GitHub Desktop.
RelationsInspector test backend, with adding and removing GameObjects
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