Created
January 19, 2016 04:52
-
-
Save seldomU/1166a89a6206894b5629 to your computer and use it in GitHub Desktop.
RelationsInspector test backend, with transform parent editing
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 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