@@ -21,31 +21,28 @@ namespace UnityExplorer.UI.Widgets
21
21
public class Texture2DWidget : UnityObjectWidget
22
22
{
23
23
private Texture2D TextureRef ;
24
+ private float realWidth ;
25
+ private float realHeight ;
24
26
25
27
private bool textureViewerWanted ;
28
+ private ButtonRef toggleButton ;
26
29
27
- private InputFieldRef textureSavePathInput ;
28
- private Image textureImage ;
29
- private LayoutElement textureImageLayout ;
30
-
31
- private ButtonRef textureButton ;
32
- private GameObject textureViewer ;
33
-
34
- private float realWidth ;
35
- private float realHeight ;
30
+ private GameObject textureViewerRoot ;
31
+ private InputFieldRef savePathInput ;
32
+ private Image image ;
33
+ private LayoutElement imageLayout ;
36
34
37
35
public override void OnBorrowed ( object target , Type targetType , ReflectionInspector inspector )
38
36
{
39
37
base . OnBorrowed ( target , targetType , inspector ) ;
40
38
41
- TextureRef = ( Texture2D ) target . TryCast ( typeof ( Texture2D ) ) ;
42
- textureButton . Component . gameObject . SetActive ( true ) ;
39
+ TextureRef = target . TryCast < Texture2D > ( ) ;
43
40
44
41
realWidth = TextureRef . width ;
45
42
realHeight = TextureRef . height ;
46
43
47
- if ( this . textureViewer )
48
- this . textureViewer . transform . SetParent ( inspector . UIRoot . transform ) ;
44
+ if ( this . textureViewerRoot )
45
+ this . textureViewerRoot . transform . SetParent ( inspector . UIRoot . transform ) ;
49
46
50
47
InspectorPanel . Instance . Dragger . OnFinishResize += OnInspectorFinishResize ;
51
48
}
@@ -56,13 +53,14 @@ public override void OnReturnToPool()
56
53
57
54
TextureRef = null ;
58
55
59
- if ( textureImage . sprite )
60
- GameObject . Destroy ( textureImage . sprite ) ;
56
+ if ( image . sprite )
57
+ GameObject . Destroy ( image . sprite ) ;
61
58
62
59
if ( textureViewerWanted )
63
60
ToggleTextureViewer ( ) ;
64
61
65
- this . textureViewer . transform . SetParent ( Pool < Texture2DWidget > . Instance . InactiveHolder . transform ) ;
62
+ if ( this . textureViewerRoot )
63
+ this . textureViewerRoot . transform . SetParent ( Pool < Texture2DWidget > . Instance . InactiveHolder . transform ) ;
66
64
67
65
base . OnReturnToPool ( ) ;
68
66
}
@@ -73,22 +71,22 @@ private void ToggleTextureViewer()
73
71
{
74
72
// disable
75
73
textureViewerWanted = false ;
76
- textureViewer . SetActive ( false ) ;
77
- textureButton . ButtonText . text = "View Texture" ;
74
+ textureViewerRoot . SetActive ( false ) ;
75
+ toggleButton . ButtonText . text = "View Texture" ;
78
76
79
77
ParentInspector . mainContentHolder . SetActive ( true ) ;
80
78
}
81
79
else
82
80
{
83
81
// enable
84
- if ( ! textureImage . sprite )
82
+ if ( ! image . sprite )
85
83
SetupTextureViewer ( ) ;
86
84
87
85
SetImageSize ( ) ;
88
86
89
87
textureViewerWanted = true ;
90
- textureViewer . SetActive ( true ) ;
91
- textureButton . ButtonText . text = "Hide Texture" ;
88
+ textureViewerRoot . SetActive ( true ) ;
89
+ toggleButton . ButtonText . text = "Hide Texture" ;
92
90
93
91
ParentInspector . mainContentHolder . gameObject . SetActive ( false ) ;
94
92
}
@@ -102,11 +100,10 @@ private void SetupTextureViewer()
102
100
string name = TextureRef . name ;
103
101
if ( string . IsNullOrEmpty ( name ) )
104
102
name = "untitled" ;
105
-
106
- textureSavePathInput . Text = Path . Combine ( ConfigManager . Default_Output_Path . Value , $ "{ name } .png") ;
103
+ savePathInput . Text = Path . Combine ( ConfigManager . Default_Output_Path . Value , $ "{ name } .png") ;
107
104
108
105
Sprite sprite = TextureHelper . CreateSprite ( TextureRef ) ;
109
- textureImage . sprite = sprite ;
106
+ image . sprite = sprite ;
110
107
}
111
108
112
109
private void OnInspectorFinishResize ( RectTransform _ )
@@ -116,6 +113,9 @@ private void OnInspectorFinishResize(RectTransform _)
116
113
117
114
private void SetImageSize ( )
118
115
{
116
+ if ( ! imageLayout )
117
+ return ;
118
+
119
119
RuntimeHelper . StartCoroutine ( SetImageSizeCoro ( ) ) ;
120
120
}
121
121
@@ -132,8 +132,8 @@ IEnumerator SetImageSizeCoro()
132
132
// If our image is smaller than the viewport, just use 100% scaling
133
133
if ( realWidth < rectWidth && realHeight < rectHeight )
134
134
{
135
- textureImageLayout . minWidth = realWidth ;
136
- textureImageLayout . minHeight = realHeight ;
135
+ imageLayout . minWidth = realWidth ;
136
+ imageLayout . minHeight = realHeight ;
137
137
}
138
138
else // we will need to scale down the image to fit
139
139
{
@@ -144,13 +144,13 @@ IEnumerator SetImageSizeCoro()
144
144
// if width needs to be scaled more than height
145
145
if ( viewWidthRatio < viewHeightRatio )
146
146
{
147
- textureImageLayout . minWidth = realWidth * viewWidthRatio ;
148
- textureImageLayout . minHeight = realHeight * viewWidthRatio ;
147
+ imageLayout . minWidth = realWidth * viewWidthRatio ;
148
+ imageLayout . minHeight = realHeight * viewWidthRatio ;
149
149
}
150
150
else // if height needs to be scaled more than width
151
151
{
152
- textureImageLayout . minWidth = realWidth * viewHeightRatio ;
153
- textureImageLayout . minHeight = realHeight * viewHeightRatio ;
152
+ imageLayout . minWidth = realWidth * viewHeightRatio ;
153
+ imageLayout . minHeight = realHeight * viewHeightRatio ;
154
154
}
155
155
}
156
156
}
@@ -159,22 +159,19 @@ private void OnSaveTextureClicked()
159
159
{
160
160
if ( ! TextureRef )
161
161
{
162
- ExplorerCore . LogWarning ( "Ref Texture is null, maybe it was destroyed?" ) ;
162
+ ExplorerCore . LogWarning ( "Texture is null, maybe it was destroyed?" ) ;
163
163
return ;
164
164
}
165
165
166
- if ( string . IsNullOrEmpty ( textureSavePathInput . Text ) )
166
+ if ( string . IsNullOrEmpty ( savePathInput . Text ) )
167
167
{
168
168
ExplorerCore . LogWarning ( "Save path cannot be empty!" ) ;
169
169
return ;
170
170
}
171
171
172
- string path = textureSavePathInput . Text ;
172
+ string path = savePathInput . Text ;
173
173
if ( ! path . EndsWith ( ".png" , StringComparison . InvariantCultureIgnoreCase ) )
174
- {
175
- ExplorerCore . LogWarning ( "Desired save path must end with '.png'!" ) ;
176
- return ;
177
- }
174
+ path += ".png" ;
178
175
179
176
path = IOUtility . EnsureValidFilePath ( path ) ;
180
177
@@ -201,49 +198,45 @@ public override GameObject CreateContent(GameObject uiRoot)
201
198
202
199
// Button
203
200
204
- textureButton = UIFactory . CreateButton ( unityObjectRow , "TextureButton" , "View Texture" , new Color ( 0.2f , 0.3f , 0.2f ) ) ;
205
- textureButton . Transform . SetSiblingIndex ( 0 ) ;
206
- UIFactory . SetLayoutElement ( textureButton . Component . gameObject , minHeight : 25 , minWidth : 150 ) ;
207
- textureButton . OnClick += ToggleTextureViewer ;
201
+ toggleButton = UIFactory . CreateButton ( UIRoot , "TextureButton" , "View Texture" , new Color ( 0.2f , 0.3f , 0.2f ) ) ;
202
+ toggleButton . Transform . SetSiblingIndex ( 0 ) ;
203
+ UIFactory . SetLayoutElement ( toggleButton . Component . gameObject , minHeight : 25 , minWidth : 150 ) ;
204
+ toggleButton . OnClick += ToggleTextureViewer ;
208
205
209
206
// Texture viewer
210
207
211
- textureViewer = UIFactory . CreateVerticalGroup ( uiRoot , "TextureViewer" , false , false , true , true , 2 , new Vector4 ( 5 , 5 , 5 , 5 ) ,
208
+ textureViewerRoot = UIFactory . CreateVerticalGroup ( uiRoot , "TextureViewer" , false , false , true , true , 2 , new Vector4 ( 5 , 5 , 5 , 5 ) ,
212
209
new Color ( 0.1f , 0.1f , 0.1f ) , childAlignment : TextAnchor . UpperLeft ) ;
213
- UIFactory . SetLayoutElement ( textureViewer , flexibleWidth : 9999 , flexibleHeight : 9999 ) ;
210
+ UIFactory . SetLayoutElement ( textureViewerRoot , flexibleWidth : 9999 , flexibleHeight : 9999 ) ;
214
211
215
212
// Save helper
216
213
217
- GameObject saveRowObj = UIFactory . CreateHorizontalGroup ( textureViewer , "SaveRow" , false , false , true , true , 2 , new Vector4 ( 2 , 2 , 2 , 2 ) ,
214
+ GameObject saveRowObj = UIFactory . CreateHorizontalGroup ( textureViewerRoot , "SaveRow" , false , false , true , true , 2 , new Vector4 ( 2 , 2 , 2 , 2 ) ,
218
215
new Color ( 0.1f , 0.1f , 0.1f ) ) ;
219
216
220
217
ButtonRef saveBtn = UIFactory . CreateButton ( saveRowObj , "SaveButton" , "Save .PNG" , new Color ( 0.2f , 0.25f , 0.2f ) ) ;
221
218
UIFactory . SetLayoutElement ( saveBtn . Component . gameObject , minHeight : 25 , minWidth : 100 , flexibleWidth : 0 ) ;
222
219
saveBtn . OnClick += OnSaveTextureClicked ;
223
220
224
- textureSavePathInput = UIFactory . CreateInputField ( saveRowObj , "SaveInput" , "..." ) ;
225
- UIFactory . SetLayoutElement ( textureSavePathInput . UIRoot , minHeight : 25 , minWidth : 100 , flexibleWidth : 9999 ) ;
221
+ savePathInput = UIFactory . CreateInputField ( saveRowObj , "SaveInput" , "..." ) ;
222
+ UIFactory . SetLayoutElement ( savePathInput . UIRoot , minHeight : 25 , minWidth : 100 , flexibleWidth : 9999 ) ;
226
223
227
224
// Actual texture viewer
228
225
229
- // GameObject imageViewport = UIFactory.CreateVerticalGroup(textureViewer , "ImageViewport", false, false, true, true);
230
- //imageRect = imageViewport.GetComponent<RectTransform>( );
231
- // UIFactory.SetLayoutElement(imageViewport, flexibleWidth: 9999, flexibleHeight: 9999);
226
+ GameObject imageViewport = UIFactory . CreateVerticalGroup ( textureViewerRoot , "ImageViewport" , false , false , true , true ,
227
+ bgColor : new ( 1 , 1 , 1 , 0 ) , childAlignment : TextAnchor . MiddleCenter ) ;
228
+ UIFactory . SetLayoutElement ( imageViewport , flexibleWidth : 9999 , flexibleHeight : 9999 ) ;
232
229
233
- GameObject imageHolder = UIFactory . CreateUIObject ( "ImageHolder" , textureViewer ) ;
234
- textureImageLayout = UIFactory . SetLayoutElement ( imageHolder , 1 , 1 , 0 , 0 ) ;
235
- imageHolder . AddComponent < Image > ( ) . color = Color . clear ;
236
- var outline = imageHolder . AddComponent < Outline > ( ) ;
237
- outline . effectColor = Color . black ;
238
- outline . effectDistance = new ( 2 , 2 ) ;
230
+ GameObject imageHolder = UIFactory . CreateUIObject ( "ImageHolder" , imageViewport ) ;
231
+ imageLayout = UIFactory . SetLayoutElement ( imageHolder , 1 , 1 , 0 , 0 ) ;
239
232
240
233
var actualImageObj = UIFactory . CreateUIObject ( "ActualImage" , imageHolder ) ;
241
234
var actualRect = actualImageObj . GetComponent < RectTransform > ( ) ;
242
235
actualRect . anchorMin = new ( 0 , 0 ) ;
243
236
actualRect . anchorMax = new ( 1 , 1 ) ;
244
- textureImage = actualImageObj . AddComponent < Image > ( ) ;
237
+ image = actualImageObj . AddComponent < Image > ( ) ;
245
238
246
- textureViewer . SetActive ( false ) ;
239
+ textureViewerRoot . SetActive ( false ) ;
247
240
248
241
return ret ;
249
242
}
0 commit comments