Skip to content

Commit a1a5e30

Browse files
committed
added functions to check if animation dest/source are valid
1 parent bf148ed commit a1a5e30

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

Assets/FbxExporters/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ protected override Transform TransferAnimationSource {
2525
return ExportSettings.instance.convertToPrefabSettings.info.AnimationSource;
2626
}
2727
set {
28+
var selectedGO = ModelExporter.GetGameObject(m_toConvert[0]);
29+
if (!TransferAnimationSourceIsValid (value, selectedGO)) {
30+
return;
31+
}
2832
ExportSettings.instance.convertToPrefabSettings.info.SetAnimationSource (value);
2933
}
3034
}
@@ -34,6 +38,10 @@ protected override Transform TransferAnimationDest {
3438
return ExportSettings.instance.convertToPrefabSettings.info.AnimationDest;
3539
}
3640
set {
41+
var selectedGO = ModelExporter.GetGameObject(m_toConvert[0]);
42+
if (!TransferAnimationDestIsValid (value, selectedGO)) {
43+
return;
44+
}
3745
ExportSettings.instance.convertToPrefabSettings.info.SetAnimationDest (value);
3846
}
3947
}

Assets/FbxExporters/Editor/ExportModelEditorWindow.cs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,74 @@ protected void ShowPresetReceiver(UnityEngine.Object target){
123123
protected abstract Transform TransferAnimationSource { get; set; }
124124
protected abstract Transform TransferAnimationDest { get; set; }
125125

126+
//-------Helper functions for determining if Animation source and dest are valid---------
127+
128+
/// <summary>
129+
/// Determines whether p is an ancestor to t.
130+
/// </summary>
131+
/// <returns><c>true</c> if p is ancestor to t; otherwise, <c>false</c>.</returns>
132+
/// <param name="p">P.</param>
133+
/// <param name="t">T.</param>
134+
protected bool IsAncestor(Transform p, Transform t){
135+
var curr = t;
136+
while (curr != null) {
137+
if (curr == p) {
138+
return true;
139+
}
140+
curr = curr.parent;
141+
}
142+
return false;
143+
}
144+
145+
/// <summary>
146+
/// Determines whether t1 and t2 are in the same hierarchy.
147+
/// </summary>
148+
/// <returns><c>true</c> if t1 is in same hierarchy as t2; otherwise, <c>false</c>.</returns>
149+
/// <param name="t1">T1.</param>
150+
/// <param name="t2">T2.</param>
151+
protected bool IsInSameHierarchy(Transform t1, Transform t2){
152+
return (IsAncestor (t1, t2) || IsAncestor (t2, t1));
153+
}
154+
155+
156+
protected virtual bool TransferAnimationSourceIsValid(Transform newValue, GameObject selectedGO){
157+
if (!newValue) {
158+
return true;
159+
}
160+
161+
// source must be ancestor to dest
162+
if (TransferAnimationDest && !IsAncestor(newValue, TransferAnimationDest)) {
163+
Debug.LogWarningFormat("FbxExportSettings: Source {0} must be an ancestor of {1}", newValue.name, TransferAnimationDest.name);
164+
return false;
165+
}
166+
// must be in same hierarchy as selected GO
167+
if (!selectedGO || !IsInSameHierarchy(newValue, selectedGO.transform)) {
168+
Debug.LogWarningFormat("FbxExportSettings: Source {0} must be in the same hierarchy as {1}", newValue.name, selectedGO? selectedGO.name : "the selected object");
169+
return false;
170+
}
171+
return true;
172+
}
173+
174+
protected virtual bool TransferAnimationDestIsValid(Transform newValue, GameObject selectedGO){
175+
if (!newValue) {
176+
return true;
177+
}
178+
179+
// source must be ancestor to dest
180+
if (TransferAnimationSource && !IsAncestor(TransferAnimationSource, newValue)) {
181+
Debug.LogWarningFormat("FbxExportSettings: Destination {0} must be a descendant of {1}", newValue.name, TransferAnimationSource.name);
182+
return false;
183+
}
184+
// must be in same hierarchy as selected GO
185+
if (!selectedGO || !IsInSameHierarchy(newValue, selectedGO.transform)) {
186+
Debug.LogWarningFormat("FbxExportSettings: Destination {0} must be in the same hierarchy as {1}", newValue.name, selectedGO? selectedGO.name : "the selected object");
187+
return false;
188+
}
189+
return true;
190+
}
191+
192+
// -------------------------------------------------------------------------------------
193+
126194
protected void OnGUI ()
127195
{
128196
// Increasing the label width so that none of the text gets cut off
@@ -308,6 +376,7 @@ protected bool IsPlayableDirector {
308376
set {
309377
m_isPlayableDirector = value;
310378
DisableNameSelection = m_isPlayableDirector;
379+
DisableTransferAnim = m_isPlayableDirector;
311380
}
312381
}
313382

@@ -316,10 +385,10 @@ protected override Transform TransferAnimationSource {
316385
return ExportSettings.instance.exportModelSettings.info.AnimationSource;
317386
}
318387
set {
319-
// source must be ancestor to dest
320-
321-
// must be in same hierarchy as selected GO
322-
388+
var selectedGO = ModelExporter.GetGameObject(m_toExport[0]);
389+
if (!TransferAnimationSourceIsValid (value, selectedGO)) {
390+
return;
391+
}
323392
ExportSettings.instance.exportModelSettings.info.SetAnimationSource (value);
324393
}
325394
}
@@ -329,6 +398,10 @@ protected override Transform TransferAnimationDest {
329398
return ExportSettings.instance.exportModelSettings.info.AnimationDest;
330399
}
331400
set {
401+
var selectedGO = ModelExporter.GetGameObject(m_toExport[0]);
402+
if (!TransferAnimationDestIsValid (value, selectedGO)) {
403+
return;
404+
}
332405
ExportSettings.instance.exportModelSettings.info.SetAnimationDest (value);
333406
}
334407
}

0 commit comments

Comments
 (0)