Skip to content

Instantly share code, notes, and snippets.

@seldomU
Created January 19, 2016 04:52
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/1166a89a6206894b5629 to your computer and use it in GitHub Desktop.
Save seldomU/1166a89a6206894b5629 to your computer and use it in GitHub Desktop.
RelationsInspector test backend, with transform parent editing
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using RelationsInspector;
using RelationsInspector.Backend;
public class TestBackend2 : MinimalBackend<GameObject, string>
{
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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment