Skip to content

Commit 9680bc6

Browse files
authored
Merge pull request #89 from Unity-Technologies/UNI-23240-autoload-during-playmode
Uni 23240 autoload during playmode
2 parents 5cd9fc1 + 5a17049 commit 9680bc6

File tree

3 files changed

+95
-15
lines changed

3 files changed

+95
-15
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// ***********************************************************************
2+
// Copyright (c) 2017 Unity Technologies. All rights reserved.
3+
//
4+
// Licensed under the ##LICENSENAME##.
5+
// See LICENSE.md file in the project root for full license information.
6+
// ***********************************************************************
7+
8+
9+
using System.Collections;
10+
using System.Collections.Generic;
11+
using UnityEngine;
12+
using UnityEditor;
13+
14+
namespace FbxExporters.Review
15+
{
16+
[CustomEditor (typeof(RotateModel))]
17+
public class EditorRotate : UnityEditor.Editor
18+
{
19+
RotateModel model;
20+
21+
public void OnEnable ()
22+
{
23+
model = (RotateModel)target;
24+
EditorApplication.update += Update;
25+
}
26+
27+
public void OnDisable ()
28+
{
29+
EditorApplication.update -= Update;
30+
}
31+
32+
void Update ()
33+
{
34+
// don't do anything in play mode
35+
if (model == null || EditorApplication.isPlaying) {
36+
return;
37+
}
38+
model.Rotate ();
39+
}
40+
}
41+
}

Assets/FbxExporters/Editor/ReviewLastSavedModel.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ namespace FbxExporters
55
{
66
namespace Review
77
{
8-
class TurnTable
8+
[UnityEditor.InitializeOnLoad]
9+
public class TurnTable
910
{
1011
const string MenuItemName = "FbxExporters/Turntable Review/Autoload Last Saved Prefab";
1112

@@ -86,8 +87,11 @@ private static Object LoadModel (string fbxFileName)
8687
if (turntableGO != null) {
8788
modelGO.transform.parent = turntableGO.transform;
8889
turntableGO.AddComponent<RotateModel> ();
90+
91+
UnityEditor.Selection.objects = new GameObject[]{ turntableGO };
8992
} else {
9093
modelGO.AddComponent<RotateModel> ();
94+
UnityEditor.Selection.objects = new GameObject[]{ modelGO };
9195
}
9296
}
9397

@@ -214,11 +218,10 @@ public static void LastSavedModel ()
214218
lightComp.shadows = LightShadows.Soft;
215219
}
216220

217-
// maximize game window and start playing
221+
// maximize game window
218222
var gameWindow = GetMainGameView();
219223
if (gameWindow) {
220224
gameWindow.maximized = true;
221-
UnityEditor.EditorApplication.isPlaying = true;
222225
} else {
223226
Debug.LogWarning ("Failed to access Game Window, please restart Unity to try again.");
224227
}
@@ -262,12 +265,12 @@ private static bool AutoUpdateEnabled ()
262265

263266
private static void UpdateLastSavedModel ()
264267
{
265-
266-
if (AutoUpdateEnabled ()) {
267-
LoadLastSavedModel ();
268-
} else {
268+
if (!AutoUpdateEnabled ()) {
269269
UnsubscribeFromEvents ();
270+
return;
270271
}
272+
273+
LoadLastSavedModel ();
271274
}
272275
}
273276
}

Assets/FbxExporters/RotateModel.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
1-
using System.Collections;
1+
// ***********************************************************************
2+
// Copyright (c) 2017 Unity Technologies. All rights reserved.
3+
//
4+
// Licensed under the ##LICENSENAME##.
5+
// See LICENSE.md file in the project root for full license information.
6+
// ***********************************************************************
7+
8+
using System.Collections;
29
using System.Collections.Generic;
310
using UnityEngine;
411

5-
public class RotateModel : MonoBehaviour {
12+
namespace FbxExporters.Review
13+
{
14+
public class RotateModel : MonoBehaviour
15+
{
16+
17+
[Tooltip ("Rotation speed in degrees/second")]
18+
[SerializeField]
19+
private float speed = 10f;
20+
21+
#if UNITY_EDITOR
22+
private float timeOfLastUpdate = float.MaxValue;
23+
#endif
24+
25+
public float GetSpeed()
26+
{
27+
return speed;
28+
}
29+
30+
public void Rotate()
31+
{
32+
float deltaTime = 0;
33+
#if UNITY_EDITOR
34+
deltaTime = Time.realtimeSinceStartup - timeOfLastUpdate;
35+
if(deltaTime <= 0){
36+
deltaTime = 0.001f;
37+
}
38+
timeOfLastUpdate = Time.realtimeSinceStartup;
39+
#else
40+
deltaTime = Time.deltaTime;
41+
#endif
42+
transform.Rotate (Vector3.up, speed * deltaTime, Space.World);
43+
}
644

7-
[Tooltip("Rotation speed in degrees/second")]
8-
[SerializeField]
9-
private float speed = 10f;
10-
11-
void Update () {
12-
transform.Rotate (Vector3.up, speed * Time.deltaTime, Space.World);
45+
void Update ()
46+
{
47+
Rotate ();
48+
}
1349
}
1450
}

0 commit comments

Comments
 (0)