Skip to content

Commit 45d54b4

Browse files
committed
Reuse Cubes if algorithm not changed, for a good performance due to no batch Instantiate call
1 parent 75b397d commit 45d54b4

5 files changed

Lines changed: 57 additions & 11 deletions

File tree

Assets/Resources/Prefabs/TreeContainer.prefab

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ GameObject:
99
serializedVersion: 6
1010
m_Component:
1111
- component: {fileID: 6963842262431084767}
12+
- component: {fileID: 1954112258795841313}
1213
m_Layer: 0
1314
m_Name: TreeContainer
1415
m_TagString: Untagged
@@ -30,3 +31,15 @@ Transform:
3031
m_Father: {fileID: 0}
3132
m_RootOrder: 0
3233
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
34+
--- !u!114 &1954112258795841313
35+
MonoBehaviour:
36+
m_ObjectHideFlags: 0
37+
m_CorrespondingSourceObject: {fileID: 0}
38+
m_PrefabInstance: {fileID: 0}
39+
m_PrefabAsset: {fileID: 0}
40+
m_GameObject: {fileID: 6963842262431084764}
41+
m_Enabled: 1
42+
m_EditorHideFlags: 0
43+
m_Script: {fileID: 11500000, guid: c6c411a18fdad4645b0d9f7106860f41, type: 3}
44+
m_Name:
45+
m_EditorClassIdentifier:

Assets/Scripts/GameManager.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class GameManager : MonoBehaviour
1515
private static GameObject _cubePrefab;
1616
private static GameObject _codeLinePanel;
1717
private static GameObject _menu;
18+
private static Material _defaultMat;
1819
private static GameManager _instance;
1920
public Slider min;
2021
public Slider max;
@@ -30,6 +31,7 @@ private void Awake()
3031
_cubePrefab = Resources.Load<GameObject>( "Prefabs/CubeContainer" );
3132
_codeLinePanel = GameObject.FindWithTag( "CodeLinePanel" );
3233
_menu = GameObject.Find( "SliderMenu" );
34+
_defaultMat = Resources.Load<Material>( "Materials/Cube" );
3335
_instance = this;
3436
}
3537

@@ -48,11 +50,18 @@ private void Update()
4850
public void GenObjects()
4951
{
5052
Rest();
51-
GenObjectsFromArray( GetUniqueRandomArray( (int)min.value, (int)max.value, (int)count.value ) );
53+
CompleteBinaryTree.ClearTree();
54+
GenObjectsFromArray( GetUniqueRandomArray( (int)min.value, (int)max.value, (int)count.value ), false );
5255
}
5356

54-
public static void GenObjectsFromArray( int[] arr )
57+
public static void GenObjectsFromArray( int[] arr, bool reuse = true )
5558
{
59+
if ( reuse )
60+
{
61+
Reuse( arr );
62+
return;
63+
}
64+
5665
Destroy( _space );
5766
_space = Instantiate( _spacePrefab );
5867

@@ -69,6 +78,17 @@ public static void GenObjectsFromArray( int[] arr )
6978
}
7079
}
7180

81+
private static void Reuse( int[] arr )
82+
{
83+
Numbers = arr;
84+
for ( var i = 0; i < Numbers.Length; i++ )
85+
{
86+
Cubes[i].transform.position = new Vector3( i * Config.HorizontalGap, 0f, 0f );
87+
Cubes[i].GetComponent<CubeController>().SetValue( Numbers[i] );
88+
CubeController.SetPillarMaterial( Cubes[i], _defaultMat );
89+
}
90+
}
91+
7292
private static int[] GetUniqueRandomArray( int minNum, int maxNum, int count )
7393
{
7494
if ( maxNum - minNum < count )
@@ -100,7 +120,6 @@ public static void EnableButtons( bool enable )
100120

101121
public static void Rest()
102122
{
103-
CompleteBinaryTree.ClearTree();
104123
PerformanceQueue.Course.Clear();
105124
PerformanceQueue.Rewind.Clear();
106125
CubeController.courseIndex = 0;

Assets/Scripts/Performance/CompleteBinaryTree.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
// license that can be found in the LICENSE file.
44

55
using System;
6+
using UI;
67
using UnityEngine;
78

89
namespace Performance
910
{
1011
public class CompleteBinaryTree : MonoBehaviour
1112
{
12-
private static readonly GameObject TreeContainerPrefab = Resources.Load<GameObject>( "Prefabs/TreeContainer" );
13-
private static GameObject _treeContainer;
14-
public static GameManager.MyList<GameObject> treeNodes = new GameManager.MyList<GameObject>();
13+
private static GameObject _treeContainer;
14+
public static GameManager.MyList<GameObject> treeNodes = new GameManager.MyList<GameObject>();
1515

1616
public static void BuildTree()
1717
{
1818
ClearTree();
19-
_treeContainer = Instantiate( TreeContainerPrefab );
19+
_treeContainer = Instantiate( Resources.Load<GameObject>( "Prefabs/TreeContainer" ) );
2020

2121
const int storeyHeight = 3;
2222
var heapSize = GameManager.Cubes.Count;
@@ -69,9 +69,22 @@ public static void BuildTree()
6969
}
7070
}
7171

72+
public static void ResetTree()
73+
{
74+
for ( var i = 0; i < treeNodes.Count; i++ )
75+
{
76+
treeNodes[i].GetComponent<CubeController>().SetValue( GameManager.Numbers[i], false );
77+
}
78+
}
79+
7280
public static void ClearTree()
7381
{
7482
Destroy( _treeContainer );
7583
}
84+
85+
private void Update()
86+
{
87+
_treeContainer.SetActive( Sort.className == "Heap" );
88+
}
7689
}
7790
}

Assets/Scripts/Performance/CubeController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ private void SetButtonText( string str )
3838
foreach ( var btn in buttons ) btn.GetComponentInChildren<Text>().text = str;
3939
}
4040

41-
public void SetValue( int value )
41+
public void SetValue( int value, bool changeScale = true )
4242
{
43-
scaler.localScale = new Vector3( 1, value * Config.CubeScale, 1 );
43+
if ( changeScale )
44+
scaler.localScale = new Vector3( 1, value * Config.CubeScale, 1 );
4445
SetButtonText( value.ToString() );
4546
_value = value;
4647
}
@@ -136,7 +137,7 @@ private static IEnumerator Show( Step step )
136137
}
137138
}
138139

139-
private static void SetPillarMaterial( GameObject go, Material mat )
140+
public static void SetPillarMaterial( GameObject go, Material mat )
140141
{
141142
go.transform.Find( "Cube" ).transform.Find( "Pillar" ).GetComponent<MeshRenderer>().material = mat;
142143
}

Assets/Scripts/UI/ProgressBar.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ private void Ending( string algorithm )
106106
switch ( algorithm )
107107
{
108108
case "Heap":
109-
CompleteBinaryTree.BuildTree();
109+
CompleteBinaryTree.ResetTree();
110110
break;
111111
case "Radix":
112112
break;

0 commit comments

Comments
 (0)