Skip to content

Commit

Permalink
Can now drag tags to inspector to add them to our object
Browse files Browse the repository at this point in the history
  • Loading branch information
DiogoDeAndrade committed Mar 10, 2023
1 parent 204f2dd commit 1661a62
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 71 deletions.
100 changes: 31 additions & 69 deletions Assets/OkapiKit/Samples/Snippets/Probe.unity
Original file line number Diff line number Diff line change
Expand Up @@ -578,35 +578,16 @@ GameObject:
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1429778259}
- component: {fileID: 1429778258}
- component: {fileID: 1429778257}
- component: {fileID: 1429778256}
- component: {fileID: 1429778255}
- component: {fileID: 1429778260}
m_Layer: 0
m_Name: Obstacle3
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1429778255
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1429778254}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f6fc8a1e9dd21634a9cb00402422ec6c, type: 3}
m_Name:
m_EditorClassIdentifier:
_showInfo: 1
_explanation:
description:
hypertags:
- {fileID: 11400000, guid: 3138ad8dc4cf4f74a98c1580707d1de8, type: 2}
--- !u!61 &1429778256
BoxCollider2D:
m_ObjectHideFlags: 0
Expand All @@ -633,27 +614,6 @@ BoxCollider2D:
serializedVersion: 2
m_Size: {x: 114, y: 22}
m_EdgeRadius: 0
--- !u!50 &1429778257
Rigidbody2D:
serializedVersion: 4
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1429778254}
m_BodyType: 2
m_Simulated: 1
m_UseFullKinematicContacts: 0
m_UseAutoMass: 0
m_Mass: 1
m_LinearDrag: 0
m_AngularDrag: 0.05
m_GravityScale: 1
m_Material: {fileID: 0}
m_Interpolate: 0
m_SleepingMode: 1
m_CollisionDetection: 0
m_Constraints: 0
--- !u!212 &1429778258
SpriteRenderer:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -706,21 +666,23 @@ SpriteRenderer:
m_WasSpriteAssigned: 1
m_MaskInteraction: 0
m_SpriteSortPoint: 0
--- !u!4 &1429778259
Transform:
--- !u!114 &1429778260
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1429778254}
m_LocalRotation: {x: -0, y: -0, z: -0.5401815, w: 0.8415485}
m_LocalPosition: {x: 431, y: 259, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: -65.392}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f6fc8a1e9dd21634a9cb00402422ec6c, type: 3}
m_Name:
m_EditorClassIdentifier:
_showInfo: 1
_explanation:
description:
hypertags:
- {fileID: 11400000, guid: 3138ad8dc4cf4f74a98c1580707d1de8, type: 2}
--- !u!1 &1507752128
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1011,31 +973,14 @@ GameObject:
- component: {fileID: 1788293541}
- component: {fileID: 1788293540}
- component: {fileID: 1788293539}
- component: {fileID: 1788293538}
- component: {fileID: 1788293543}
m_Layer: 0
m_Name: Obstacle2
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1788293538
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1788293537}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f6fc8a1e9dd21634a9cb00402422ec6c, type: 3}
m_Name:
m_EditorClassIdentifier:
_showInfo: 1
_explanation:
description:
hypertags:
- {fileID: 11400000, guid: 3138ad8dc4cf4f74a98c1580707d1de8, type: 2}
--- !u!61 &1788293539
BoxCollider2D:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -1150,6 +1095,23 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: -65.392}
--- !u!114 &1788293543
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1788293537}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f6fc8a1e9dd21634a9cb00402422ec6c, type: 3}
m_Name:
m_EditorClassIdentifier:
_showInfo: 1
_explanation:
description:
hypertags:
- {fileID: 11400000, guid: 3138ad8dc4cf4f74a98c1580707d1de8, type: 2}
--- !u!1 &2027386094
GameObject:
m_ObjectHideFlags: 0
Expand Down
129 changes: 129 additions & 0 deletions Assets/OkapiKit/Scripts/Hypertag/Editor/DragAndDropTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public static class DragAndDropTag
{
static List<Hypertag> currentTags;

static DragAndDropTag()
{
//SceneView.duringSceneGui += DragAndDropHypertag;
EditorApplication.update += DragAndDropHypertag;
}

private static void DragAndDropHypertag()
{
if (EditorWindow.mouseOverWindow == null) return;

if (EditorWindow.mouseOverWindow.titleContent.text == "Inspector")
{
// Check if an object is being dragged, and if it is an hypertag
bool checkIfHypertag = true;
List<Hypertag> tags = new List<Hypertag>();
foreach (UnityEngine.Object obj in DragAndDrop.objectReferences)
{
if (obj is not Hypertag)
{
checkIfHypertag = false;
break;
}
else
{
tags.Add(obj as Hypertag);
}
}

if ((checkIfHypertag) && (tags.Count > 0))
{
if (Selection.gameObjects.Length > 0)
{
currentTags = tags;
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
}
}
else if (DragAndDrop.objectReferences.Length == 0)
{
if ((currentTags != null) && (Selection.gameObjects.Length > 0))
{
// There's tags to drop on current object
foreach (var obj in Selection.gameObjects)
{
HypertaggedObject ho = obj.GetComponent<HypertaggedObject>();
if (ho == null)
{
Undo.RecordObject(obj, "Add Hypertag");
ho = Undo.AddComponent<HypertaggedObject>(obj);
Undo.RegisterCompleteObjectUndo(obj, "Add Hypertag");
}

Undo.RecordObject(ho, "Change Hypertags");
ho.AddTag(currentTags);
Undo.RegisterCompleteObjectUndo(ho, "Changed Hypertags");
}

Undo.FlushUndoRecordObjects();
}
currentTags = null;
}
}
else
{
if (currentTags != null)
{
currentTags = null;
DragAndDrop.visualMode = DragAndDropVisualMode.Rejected;
}
}
}

/* private static void DragAndDropHypertag(SceneView sceneView)
{
Event evt = Event.current;
if ((evt.type == EventType.DragUpdated || evt.type == EventType.DragPerform))
// && (rect.Contains(evt.mousePosition)))
{
bool checkIfHypertag = true;
foreach (UnityEngine.Object obj in DragAndDrop.objectReferences)
{
if (obj is not Hypertag)
{
checkIfHypertag = false;
break;
}
}
if (checkIfHypertag)
{
Debug.Log("Dragging hypertag");
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (evt.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
foreach (Object obj in DragAndDrop.objectReferences)
{
if (obj is Action)
{
// Add element to the array
actionList.arraySize++;
var newElement = actionList.GetArrayElementAtIndex(actionList.arraySize - 1);
if (newElement != null)
{
var propDelay = newElement.FindPropertyRelative("delay");
var propAction = newElement.FindPropertyRelative("action");
if (propDelay != null) propDelay.floatValue = d;
if (propAction != null) propAction.objectReferenceValue = obj as Action;
}
}
}
}
evt.Use();
}
}
}*/
}
11 changes: 11 additions & 0 deletions Assets/OkapiKit/Scripts/Hypertag/Editor/DragAndDropTag.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;

[CustomEditor(typeof(HypertaggedObject))]
[CanEditMultipleObjects]
public class HypertaggedObjectEditor : OkapiBaseEditor
{
SerializedProperty propHypertags;
Expand Down
30 changes: 28 additions & 2 deletions Assets/OkapiKit/Scripts/Hypertag/HypertaggedObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
public class HypertaggedObject : OkapiElement
{
[SerializeField]
private Hypertag[] hypertags;
private List<Hypertag> hypertags;

public string GetTagString()
{
if ((hypertags == null) || (hypertags.Length == 0)) return "Hypertag";
if ((hypertags == null) || (hypertags.Count == 0)) return "Hypertag";

string ret = "";
foreach (var tag in hypertags)
Expand All @@ -24,6 +24,32 @@ public string GetTagString()

return ret;
}

public void AddTag(Hypertag tag)
{
if (hypertags == null) hypertags = new List<Hypertag>();

if (!hypertags.Contains(tag)) hypertags.Add(tag);
}

public void AddTag(Hypertag[] tags)
{
foreach (var t in tags)
{
AddTag(t);
}
}

public void AddTag(List<Hypertag> tags)
{
foreach (var t in tags)
{
AddTag(t);
}
}



public bool Has(Hypertag tag)
{
if (hypertags == null) return false;
Expand Down

0 comments on commit 1661a62

Please sign in to comment.