Skip to content

Commit

Permalink
Fix issues in project workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
mtschoen committed Aug 16, 2020
1 parent 5e27a34 commit d56500a
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 115 deletions.
112 changes: 108 additions & 4 deletions Runtime/Scripts/Data/FolderData.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,130 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Unity.ListViewFramework;
using UnityEditor;
using UnityEngine;

namespace Unity.EditorXR.Data
{
sealed class FolderData : NestedListViewItemData<FolderData, int>
{
const string k_TemplateName = "FolderListItem";

readonly List<AssetData> m_Assets;
// Maximum time (in ms) before yielding in CreateFolderData: should be target frame time
const float k_MaxFrameTime = 0.01f;

// Minimum time to spend loading the project folder before yielding
const float k_MinFrameTime = 0.005f;
static float s_ProjectFolderLoadStartTime;
static float s_ProjectFolderLoadYieldTime;

List<AssetData> m_Assets;
readonly int m_Index;
int m_Depth;

public string name { get; private set; }
public List<AssetData> assets { get { return m_Assets; } }
public override int index { get { return m_Index; } }
public override string template { get { return k_TemplateName; } }

public FolderData(string name, List<FolderData> children, List<AssetData> assets, int guid)
public FolderData(string name, int guid, int depth)
{
this.name = name;
m_Index = guid;
m_Children = children;
m_Assets = assets;
m_Depth = depth;
}

public static IEnumerator CreateRootFolderData(HashSet<string> assetTypes, Action<FolderData> callback)
{
var hp = new HierarchyProperty(HierarchyType.Assets);
hp.SetSearchFilter("t:object", 0);

var folderStack = new Stack<FolderData>();
var folder = new FolderData(hp.name, hp.guid.GetHashCode(), hp.depth);
while (hp.Next(null))
{
while (hp.depth <= folder.m_Depth)
folder = folderStack.Pop();

if (hp.isFolder)
{
var folderList = folder.m_Children;
if (folderList == null)
{
folderList = new List<FolderData>();
folder.m_Children = folderList;
}

folderStack.Push(folder);
folder = new FolderData(hp.name, hp.guid.GetHashCode(), hp.depth);
folderList.Add(folder);
}
else if (hp.isMainRepresentation) // Ignore sub-assets (mixer children, terrain splats, etc.)
{
var assetList = folder.m_Assets;
if (assetList == null)
{
assetList = new List<AssetData>();
folder.m_Assets = assetList;
}

assetList.Add(CreateAssetData(hp, assetTypes));
}

// Spend a minimum amount of time in this function, and if we have extra time in the frame, use it
var time = Time.realtimeSinceStartup;
if (time - s_ProjectFolderLoadYieldTime > k_MaxFrameTime
&& time - s_ProjectFolderLoadStartTime > k_MinFrameTime)
{
s_ProjectFolderLoadYieldTime = time;
yield return null;
s_ProjectFolderLoadStartTime = time;
}
}

while (folderStack.Count > 0)
folder = folderStack.Pop();

callback(folder);
}

static AssetData CreateAssetData(HierarchyProperty hp, HashSet<string> assetTypes = null)
{
var typeName = string.Empty;
if (assetTypes != null)
{
var path = AssetDatabase.GUIDToAssetPath(hp.guid);
if (Path.GetExtension(path) == ".asset") // Some .assets cause a hitch when getting their type
{
typeName = "Asset";
}
else
{
var type = AssetDatabase.GetMainAssetTypeAtPath(path);
if (type != null)
{
typeName = type.Name;
switch (typeName)
{
case "MonoScript":
typeName = "Script";
break;
case "SceneAsset":
typeName = "Scene";
break;
case "AudioMixerController":
typeName = "AudioMixer";
break;
}
}
}

assetTypes.Add(typeName);
}

return new AssetData(hp.name, hp.guid, typeName);
}
}
}
116 changes: 10 additions & 106 deletions Runtime/Scripts/Modules/ProjectFolderModule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Unity.EditorXR.Core;
using Unity.EditorXR.Data;
Expand All @@ -14,12 +11,6 @@ namespace Unity.EditorXR.Modules
#if UNITY_EDITOR
sealed class ProjectFolderModule : MonoBehaviour, IDelayedInitializationModule, IInterfaceConnector
{
// Maximum time (in ms) before yielding in CreateFolderData: should be target frame time
const float k_MaxFrameTime = 0.01f;

// Minimum time to spend loading the project folder before yielding
const float k_MinProjectFolderLoadTime = 0.005f;

readonly List<IFilterUI> m_FilterUIs = new List<IFilterUI>();

readonly List<IUsesProjectFolderData> m_ProjectFolderLists = new List<IUsesProjectFolderData>();
Expand Down Expand Up @@ -82,111 +73,24 @@ List<FolderData> GetFolderData()
void UpdateProjectFolders()
{
m_AssetTypes.Clear();

StartCoroutine(CreateFolderData((folderData, hasNext) =>
{
m_FolderData = new List<FolderData> { folderData };

// Send new data to existing folderLists
foreach (var list in m_ProjectFolderLists)
{
list.folderData = GetFolderData();
}

// Send new data to existing filterUIs
foreach (var filterUI in m_FilterUIs)
{
filterUI.filterList = GetFilterList();
}
}, m_AssetTypes));
StartCoroutine(FolderData.CreateRootFolderData(m_AssetTypes, SetupFolderData));
}

IEnumerator CreateFolderData(Action<FolderData, bool> callback, HashSet<string> assetTypes, bool hasNext = true, HierarchyProperty hp = null)
void SetupFolderData(FolderData folderData)
{
if (hp == null)
{
hp = new HierarchyProperty(HierarchyType.Assets);
hp.SetSearchFilter("t:object", 0);
}
m_FolderData = new List<FolderData> { folderData };

var name = hp.name;
var guid = hp.guid.GetHashCode();
var depth = hp.depth;
var folderList = new List<FolderData>();
var assetList = new List<AssetData>();
if (hasNext)
// Send new data to existing folderLists
foreach (var list in m_ProjectFolderLists)
{
hasNext = hp.Next(null);
while (hasNext && hp.depth > depth)
{
if (hp.isFolder)
{
yield return StartCoroutine(CreateFolderData((data, next) =>
{
folderList.Add(data);
hasNext = next;
}, assetTypes, hasNext, hp));
}
else if (hp.isMainRepresentation) // Ignore sub-assets (mixer children, terrain splats, etc.)
{
assetList.Add(CreateAssetData(hp, assetTypes));
}

if (hasNext)
hasNext = hp.Next(null);

// Spend a minimum amount of time in this function, and if we have extra time in the frame, use it
if (Time.realtimeSinceStartup - m_ProjectFolderLoadYieldTime > k_MaxFrameTime
&& Time.realtimeSinceStartup - m_ProjectFolderLoadStartTime > k_MinProjectFolderLoadTime)
{
m_ProjectFolderLoadYieldTime = Time.realtimeSinceStartup;
yield return null;
m_ProjectFolderLoadStartTime = Time.realtimeSinceStartup;
}
}

if (hasNext)
hp.Previous(null);
list.folderData = GetFolderData();
}

callback(new FolderData(name, folderList.Count > 0 ? folderList : null, assetList, guid), hasNext);
}

static AssetData CreateAssetData(HierarchyProperty hp, HashSet<string> assetTypes = null)
{
var typeName = string.Empty;
if (assetTypes != null)
// Send new data to existing filterUIs
foreach (var filterUI in m_FilterUIs)
{
var path = AssetDatabase.GUIDToAssetPath(hp.guid);
if (Path.GetExtension(path) == ".asset") // Some .assets cause a hitch when getting their type
{
typeName = "Asset";
}
else
{
var type = AssetDatabase.GetMainAssetTypeAtPath(path);
if (type != null)
{
typeName = type.Name;
switch (typeName)
{
case "MonoScript":
typeName = "Script";
break;
case "SceneAsset":
typeName = "Scene";
break;
case "AudioMixerController":
typeName = "AudioMixer";
break;
}
}
}

assetTypes.Add(typeName);
filterUI.filterList = GetFilterList();
}

return new AssetData(hp.name, hp.guid, typeName);
}

public void LoadModule() { }
Expand Down Expand Up @@ -224,4 +128,4 @@ sealed class ProjectFolderModule : MonoBehaviour
{
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ MonoBehaviour:
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_enableWordWrapping: 0
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_overflowMode: 1
m_firstOverflowCharacterIndex: -1
m_linkedTextComponent: {fileID: 0}
m_isLinkedTextComponent: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ MonoBehaviour:
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_enableWordWrapping: 0
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_overflowMode: 1
m_firstOverflowCharacterIndex: -1
m_linkedTextComponent: {fileID: 0}
m_isLinkedTextComponent: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ protected override float listHeight
if (m_NumPerRow == 0)
return 0;

var numRows = Mathf.CeilToInt(m_Data.Count / m_NumPerRow);
if (m_Data == null)
return 0;

var numRows = Mathf.CeilToInt((float)m_Data.Count / m_NumPerRow);
return Mathf.Clamp(numRows, 1, int.MaxValue) * itemSize.z;
}
}
Expand Down

0 comments on commit d56500a

Please sign in to comment.