Skip to content

Commit 2167d02

Browse files
committed
pref: Instantiate to another scene
1 parent 03b38a9 commit 2167d02

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Util.cs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Linq;
1010
using System.Collections.Generic;
1111
using System.Text;
12+
using Unity.VisualScripting;
1213
using UnityEngine;
1314
using UnityEngine.SceneManagement;
1415
using PeterVuorela.Tweener;
@@ -856,7 +857,8 @@ public static void ReattachSelf(RectTransform trans, ReattachCallback callback)
856857
/// <returns></returns>
857858
public static GameObject Instantiate(string objectPath, Vector3 position = new Vector3(), Quaternion rotation = new Quaternion())
858859
{
859-
return MonoBehaviour.Instantiate(Resources.Load<GameObject>(objectPath), position, rotation) as GameObject;
860+
var original = Resources.Load<GameObject>(objectPath);
861+
return MonoBehaviour.Instantiate(original, position, rotation);
860862
}
861863

862864
/// <summary>
@@ -866,7 +868,7 @@ public static void ReattachSelf(RectTransform trans, ReattachCallback callback)
866868
/// <param name="position"></param>
867869
/// <param name="rotation"></param>
868870
/// <returns></returns>
869-
public static UnityEngine.Object Instantiate(UnityEngine.Object trans, Vector3 position = new Vector3(), Quaternion rotation = new Quaternion())
871+
public static Object Instantiate(Object trans, Vector3 position = new Vector3(), Quaternion rotation = new Quaternion())
870872
{
871873
if (trans == null) return null;
872874
return MonoBehaviour.Instantiate(trans, position, rotation);
@@ -878,47 +880,20 @@ public static void ReattachSelf(RectTransform trans, ReattachCallback callback)
878880
/// <param name="original"> The original game object. </param>
879881
/// <param name="scene"> The target scene </param>
880882
/// <returns> Return the newly spawned game object. </returns>
881-
public static GameObject InstantiateToScene(GameObject original, Scene scene)
883+
public static Object InstantiateToScene(Object original, Scene scene, Vector3 position, Quaternion rotation)
882884
{
883-
GameObject newObj = null;
885+
Object obj = InstantiateToScene(original, scene);
884886

885-
WithActiveScene(scene, () =>
886-
{
887-
newObj = MonoBehaviour.Instantiate(original);
887+
var trans = obj.GetComponent<Transform>();
888888

889-
RemoveCloneString(newObj);
890-
});
889+
trans.position = position;
890+
trans.rotation = rotation;
891891

892-
return newObj;
892+
return obj;
893893
}
894-
895-
/// <summary>
896-
/// Execute within the active scene without losing the
897-
/// current scene.
898-
/// </summary>
899-
/// <param name="scene"> Target scene we want to execute. </param>
900-
/// <param name="action"> The execution body. </param>
901-
public static void WithActiveScene(Scene scene, System.Action action)
894+
public static Object InstantiateToScene(Object original, Scene scene)
902895
{
903-
Scene oldScene = SceneManager.GetActiveScene();
904-
905-
// If the same scene, just execute and leave.
906-
if (oldScene == scene)
907-
{
908-
if (action != null)
909-
action.Invoke();
910-
911-
return;
912-
}
913-
914-
// Switch to new scene.
915-
SceneManager.SetActiveScene(scene);
916-
917-
if (action != null)
918-
action.Invoke();
919-
920-
// Revert back to old scene.
921-
SceneManager.SetActiveScene(oldScene);
896+
return MonoBehaviour.Instantiate(original, scene);
922897
}
923898

924899
/// <summary>
@@ -1287,6 +1262,35 @@ public static bool IsSceneExists(string name)
12871262
return false;
12881263
}
12891264

1265+
/// <summary>
1266+
/// Execute within the active scene without losing the
1267+
/// current scene.
1268+
/// </summary>
1269+
/// <param name="scene"> Target scene we want to execute. </param>
1270+
/// <param name="action"> The execution body. </param>
1271+
public static void WithActiveScene(Scene scene, System.Action action)
1272+
{
1273+
Scene oldScene = SceneManager.GetActiveScene();
1274+
1275+
// If the same scene, just execute and leave.
1276+
if (oldScene == scene)
1277+
{
1278+
if (action != null)
1279+
action.Invoke();
1280+
1281+
return;
1282+
}
1283+
1284+
// Switch to new scene.
1285+
SceneManager.SetActiveScene(scene);
1286+
1287+
if (action != null)
1288+
action.Invoke();
1289+
1290+
// Revert back to old scene.
1291+
SceneManager.SetActiveScene(oldScene);
1292+
}
1293+
12901294
#endregion
12911295

12921296
#region Animation

0 commit comments

Comments
 (0)