Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 9f23c84

Browse files
Merge pull request #479 from github-for-unity/fixes/branches-view-remove-serialized-texture2d
Not attempting to serialize Texture2D objects and reloading Icons
2 parents 95586c5 + 7839904 commit 9f23c84

File tree

4 files changed

+106
-87
lines changed

4 files changed

+106
-87
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,6 @@
77

88
namespace GitHub.Unity
99
{
10-
[Serializable]
11-
public class SerializableTexture2D
12-
{
13-
[SerializeField] private byte[] bytes;
14-
[SerializeField] private int height;
15-
[SerializeField] private int width;
16-
[SerializeField] private TextureFormat format;
17-
[SerializeField] private bool mipmap;
18-
[SerializeField] private Texture2D texture;
19-
20-
public Texture2D Texture
21-
{
22-
get
23-
{
24-
if (texture == null)
25-
{
26-
texture = new Texture2D(width, height, format, mipmap);
27-
texture.LoadRawTextureData(bytes);
28-
texture.Apply();
29-
}
30-
return texture;
31-
}
32-
set
33-
{
34-
texture = value;
35-
bytes = value.GetRawTextureData();
36-
height = value.height;
37-
width = value.width;
38-
format = value.format;
39-
mipmap = value.mipmapCount > 1;
40-
}
41-
}
42-
}
43-
4410
class Utility : ScriptableObject
4511
{
4612
public static Texture2D GetIcon(string filename, string filename2x = "")
@@ -50,12 +16,25 @@ public static Texture2D GetIcon(string filename, string filename2x = "")
5016
filename = filename2x;
5117
}
5218

19+
Texture2D texture2D = null;
20+
5321
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("GitHub.Unity.IconsAndLogos." + filename);
5422
if (stream != null)
55-
return stream.ToTexture2D();
23+
{
24+
texture2D = stream.ToTexture2D();
25+
}
26+
else
27+
{
28+
var iconPath = EntryPoint.Environment.ExtensionInstallPath.Combine("IconsAndLogos", filename).ToString(SlashMode.Forward);
29+
texture2D = AssetDatabase.LoadAssetAtPath<Texture2D>(iconPath);
30+
}
31+
32+
if (texture2D != null)
33+
{
34+
texture2D.hideFlags = HideFlags.HideAndDontSave;
35+
}
5636

57-
var iconPath = EntryPoint.Environment.ExtensionInstallPath.Combine("IconsAndLogos", filename).ToString(SlashMode.Forward);
58-
return AssetDatabase.LoadAssetAtPath<Texture2D>(iconPath);
37+
return texture2D;
5938
}
6039

6140
public static Texture2D GetTextureFromColor(Color color)

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/BranchesView.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class BranchesView : Subview
3939
[NonSerialized] private int listID = -1;
4040
[NonSerialized] private BranchesMode targetMode;
4141

42-
[SerializeField] private Tree treeLocals = new Tree();
43-
[SerializeField] private Tree treeRemotes = new Tree();
42+
[SerializeField] private BranchesTree treeLocals;
43+
[SerializeField] private BranchesTree treeRemotes;
4444
[SerializeField] private BranchesMode mode = BranchesMode.Default;
4545
[SerializeField] private string newBranchName;
4646
[SerializeField] private Vector2 scroll;
@@ -62,6 +62,7 @@ public override void InitializeView(IView parent)
6262
public override void OnEnable()
6363
{
6464
base.OnEnable();
65+
UpdateTreeIcons();
6566
AttachHandlers(Repository);
6667
Repository.CheckLocalAndRemoteBranchListChangedEvent(lastLocalAndRemoteBranchListChangedEvent);
6768
}
@@ -137,25 +138,35 @@ private void Render()
137138

138139
private void BuildTree()
139140
{
141+
if (treeLocals == null)
142+
{
143+
treeLocals = new BranchesTree();
144+
treeRemotes = new BranchesTree();
145+
146+
UpdateTreeIcons();
147+
}
148+
140149
localBranches.Sort(CompareBranches);
141150
remoteBranches.Sort(CompareBranches);
142-
treeLocals = new Tree();
143-
treeLocals.ActiveNodeIcon = Styles.ActiveBranchIcon;
144-
treeLocals.NodeIcon = Styles.BranchIcon;
145-
treeLocals.RootFolderIcon = Styles.RootFolderIcon;
146-
treeLocals.FolderIcon = Styles.FolderIcon;
147-
148-
treeRemotes = new Tree();
149-
treeRemotes.ActiveNodeIcon = Styles.ActiveBranchIcon;
150-
treeRemotes.NodeIcon = Styles.BranchIcon;
151-
treeRemotes.RootFolderIcon = Styles.RootFolderIcon;
152-
treeRemotes.FolderIcon = Styles.FolderIcon;
153151

154152
treeLocals.Load(localBranches.Cast<ITreeData>(), LocalTitle);
155153
treeRemotes.Load(remoteBranches.Cast<ITreeData>(), RemoteTitle);
156154
Redraw();
157155
}
158156

157+
private void UpdateTreeIcons()
158+
{
159+
if (treeLocals != null)
160+
{
161+
treeLocals.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.RootFolderIcon);
162+
}
163+
164+
if (treeRemotes != null)
165+
{
166+
treeRemotes.UpdateIcons(Styles.ActiveBranchIcon, Styles.BranchIcon, Styles.FolderIcon, Styles.RootFolderIcon);
167+
}
168+
}
169+
159170
private void OnButtonBarGUI()
160171
{
161172
if (mode == BranchesMode.Default)

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/TreeControl.cs

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,14 @@ namespace GitHub.Unity
1212
public class TreeNodeDictionary : SerializableDictionary<string, TreeNode> { }
1313

1414
[Serializable]
15-
public class Tree
15+
public abstract class Tree
1616
{
1717
public static float ItemHeight { get { return EditorGUIUtility.singleLineHeight; } }
1818
public static float ItemSpacing { get { return EditorGUIUtility.standardVerticalSpacing; } }
1919

2020
[SerializeField] public Rect Margin = new Rect();
2121
[SerializeField] public Rect Padding = new Rect();
2222

23-
[SerializeField] private SerializableTexture2D activeNodeIcon = new SerializableTexture2D();
24-
public Texture2D ActiveNodeIcon { get { return activeNodeIcon.Texture; } set { activeNodeIcon.Texture = value; } }
25-
26-
[SerializeField] private SerializableTexture2D nodeIcon = new SerializableTexture2D();
27-
public Texture2D NodeIcon { get { return nodeIcon.Texture; } set { nodeIcon.Texture = value; } }
28-
29-
[SerializeField] private SerializableTexture2D folderIcon = new SerializableTexture2D();
30-
public Texture2D FolderIcon { get { return folderIcon.Texture; } set { folderIcon.Texture = value; } }
31-
32-
[SerializeField] private SerializableTexture2D rootFolderIcon = new SerializableTexture2D();
33-
public Texture2D RootFolderIcon { get { return rootFolderIcon.Texture; } set { rootFolderIcon.Texture = value; } }
34-
3523
[SerializeField] public GUIStyle FolderStyle;
3624
[SerializeField] public GUIStyle TreeNodeStyle;
3725
[SerializeField] public GUIStyle ActiveTreeNodeStyle;
@@ -77,7 +65,7 @@ public void Load(IEnumerable<ITreeData> data, string title)
7765
Level = 0,
7866
IsFolder = true
7967
};
80-
titleNode.Load();
68+
SetNodeIcon(titleNode);
8169
nodes.Add(titleNode);
8270

8371
var hideChildren = false;
@@ -121,9 +109,7 @@ public void Load(IEnumerable<ITreeData> data, string title)
121109
}
122110
}
123111

124-
ResetNodeIcons(node);
125-
126-
node.Load();
112+
SetNodeIcon(node);
127113

128114
nodes.Add(node);
129115
if (isFolder)
@@ -156,7 +142,6 @@ public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = nul
156142
rect = new Rect(0f, rect.y, rect.width, ItemHeight);
157143

158144
var titleNode = nodes[0];
159-
ResetNodeIcons(titleNode);
160145
bool selectionChanged = titleNode.Render(rect, 0f, selectedNode == titleNode, FolderStyle, TreeNodeStyle, ActiveTreeNodeStyle);
161146

162147
if (selectionChanged)
@@ -174,8 +159,6 @@ public Rect Render(Rect rect, Vector2 scroll, Action<TreeNode> singleClick = nul
174159
for (; i < nodes.Count; i++)
175160
{
176161
var node = nodes[i];
177-
ResetNodeIcons(node);
178-
179162
if (node.Level > level && !node.IsHidden)
180163
{
181164
Indent();
@@ -395,24 +378,20 @@ private void Unindent()
395378
indents.Pop();
396379
}
397380

398-
private void ResetNodeIcons(TreeNode node)
381+
private void SetNodeIcon(TreeNode node)
399382
{
400-
if (node.IsActive)
401-
{
402-
node.Icon = ActiveNodeIcon;
403-
}
404-
else if (node.IsFolder)
405-
{
406-
if (node.Level == 1)
407-
node.Icon = RootFolderIcon;
408-
else
409-
node.Icon = FolderIcon;
410-
}
411-
else
383+
node.Icon = GetNodeIcon(node);
384+
node.Load();
385+
}
386+
387+
protected abstract Texture2D GetNodeIcon(TreeNode node);
388+
389+
protected void LoadNodeIcons()
390+
{
391+
foreach (var treeNode in nodes)
412392
{
413-
node.Icon = NodeIcon;
393+
SetNodeIcon(treeNode);
414394
}
415-
node.Load();
416395
}
417396
}
418397

@@ -427,7 +406,7 @@ public class TreeNode
427406
public bool IsHidden;
428407
public bool IsActive;
429408
public GUIContent content;
430-
public Texture2D Icon;
409+
[NonSerialized] public Texture2D Icon;
431410

432411
public void Load()
433412
{
@@ -484,4 +463,48 @@ public override string ToString()
484463
Name, Label, Level, IsFolder, IsCollapsed, IsHidden, IsActive);
485464
}
486465
}
466+
467+
[Serializable]
468+
public class BranchesTree: Tree
469+
{
470+
[NonSerialized] public Texture2D ActiveNodeIcon;
471+
[NonSerialized] public Texture2D NodeIcon;
472+
[NonSerialized] public Texture2D FolderIcon;
473+
[NonSerialized] public Texture2D RootFolderIcon;
474+
475+
protected override Texture2D GetNodeIcon(TreeNode node)
476+
{
477+
Texture2D nodeIcon;
478+
if (node.IsActive)
479+
{
480+
nodeIcon = ActiveNodeIcon;
481+
}
482+
else if (node.IsFolder)
483+
{
484+
if (node.Level == 1)
485+
nodeIcon = RootFolderIcon;
486+
else
487+
nodeIcon = FolderIcon;
488+
}
489+
else
490+
{
491+
nodeIcon = NodeIcon;
492+
}
493+
return nodeIcon;
494+
}
495+
496+
public void UpdateIcons(Texture2D activeBranchIcon, Texture2D branchIcon, Texture2D folderIcon, Texture2D rootFolderIcon)
497+
{
498+
var needsLoad = ActiveNodeIcon == null || NodeIcon == null || FolderIcon == null || RootFolderIcon == null;
499+
if (needsLoad)
500+
{
501+
ActiveNodeIcon = activeBranchIcon;
502+
NodeIcon = branchIcon;
503+
FolderIcon = folderIcon;
504+
RootFolderIcon = rootFolderIcon;
505+
506+
LoadNodeIcons();
507+
}
508+
}
509+
}
487510
}

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/Window.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Window : BaseWindow
4040
[SerializeField] private CacheUpdateEvent lastCurrentBranchAndRemoteChangedEvent;
4141
[NonSerialized] private bool currentBranchAndRemoteHasUpdate;
4242

43+
[NonSerialized] private Texture2D smallLogo;
44+
4345
[MenuItem(LaunchMenu)]
4446
public static void Window_GitHub()
4547
{
@@ -102,7 +104,11 @@ public override void OnEnable()
102104
Selection.activeObject = this;
103105
#endif
104106
// Set window title
105-
titleContent = new GUIContent(Title, Styles.SmallLogo);
107+
if (smallLogo == null)
108+
{
109+
smallLogo = Styles.SmallLogo;
110+
titleContent = new GUIContent(Title, smallLogo);
111+
}
106112

107113
if (Repository != null)
108114
Repository.CheckCurrentBranchAndRemoteChangedEvent(lastCurrentBranchAndRemoteChangedEvent);

0 commit comments

Comments
 (0)