2020. 7. 30. 19:19
Unity/C#
씬 이동시에도 파괴 안됨
DontDestroyOnLoad(gameObject);
씬로드 v1~v5
더보기
씬 로드 Object형(에디터상에서만 먹힘)
public void LoadScene(Object SceneFile)
{
UnityEngine.SceneManagement.SceneManager.LoadScene(SceneFile.name);
}
유니티가 씬 로드에 있어 워낙 불친절한놈이라 아래처럼 수동으로 짜줘야함
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(ScenePicker), true)]
public class ScenePickerEditor : Editor
{
public override void OnInspectorGUI()
{
var picker = target as ScenePicker;
var oldScene = AssetDatabase.LoadAssetAtPath<SceneAsset>(picker.scenePath);
serializedObject.Update();
EditorGUI.BeginChangeCheck();
var newScene = EditorGUILayout.ObjectField("scene", oldScene, typeof(SceneAsset), false) as SceneAsset;
if (EditorGUI.EndChangeCheck())
{
var newPath = AssetDatabase.GetAssetPath(newScene);
var scenePathProperty = serializedObject.FindProperty("scenePath");
scenePathProperty.stringValue = newPath;
}
serializedObject.ApplyModifiedProperties();
}
}
public class ScenePicker : MonoBehaviour
{
[SerializeField]
public string scenePath;
}
축약버전
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(ScenePicker), true)]
public class ScenePickerEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
{
var target_ScenePicker = target as ScenePicker;
var oldScene = AssetDatabase.LoadAssetAtPath<Object>(target_ScenePicker.scenePath);
var newScene = EditorGUILayout.ObjectField(nameof(target_ScenePicker.scenePath), oldScene, typeof(Object), false);
target_ExcelReader.scenePath = AssetDatabase.GetAssetPath(newScene);
}
serializedObject.ApplyModifiedProperties();
}
}
public class ScenePicker : MonoBehaviour
{
[SerializeField]
public string scenePath;
}
그냥 오브젝트를 추가한 버전
이딴 버전을 왜 만들었냐 하면 인스펙터상에서 파일을 추적할때는 오브젝트를 써야하고
인 게임에서 파일을 추적할때는 string을 써야하기 때문이다
[HideInInspector]는 쓰면 안 된다. 값이 날아가 버리기 때문에(아닌가?)
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(ScenePicker), true)]
public class ScenePickerEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
{
var target_ScenePicker = target as ScenePicker;
if(target_ScenePicker.scene!=null)
{
target_ScenePicker.scenePath = AssetDatabase.GetAssetPath(target_ScenePicker.scene);
}
}
serializedObject.ApplyModifiedProperties();
}
}
public class ScenePicker : MonoBehaviour
{
public Object scene;
public string scenePath;
}
위랑 똑같은데 Path가 아닌 이름을 쓰는 버전
어차피 이래해도 작동은 잘 된다. 결국 원점회귀
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(ScenePicker), true)]
public class ScenePickerEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
serializedObject.Update();
{
var target_ScenePicker = target as ScenePicker;
if(target_ScenePicker.scene!=null)
{
target_ScenePicker.sceneName = target_ScenePicker.scene.name;
}
}
serializedObject.ApplyModifiedProperties();
}
}
#endif
public class ScenePicker : MonoBehaviour
{
#if UNITY_EDITOR
public SceneAsset scene;
#endif
[HideInInspector]
public string sceneName;
}
v5
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(ScenePicker), true)]
public class ScenePickerEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
{
DrawPropertiesExcluding(serializedObject, nameof(ScenePicker.sceneName));
var scene = serializedObject.FindProperty(nameof(ScenePicker.scene));
var sceneName = serializedObject.FindProperty(nameof(ScenePicker.sceneName));
sceneName.stringValue = scene.objectReferenceValue.name;
}
serializedObject.ApplyModifiedProperties();
}
}
#endif
public class ScenePicker : MonoBehaviour
{
#if UNITY_EDITOR
public SceneAsset scene;
#endif
[HideInInspector]
public string sceneName;
}
씬로드 v6
v5랑 동일하지만 바로바로 사용하기 편하게 함수내장시켰다
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(SceneLoader), true)]
public class SceneLoaderEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
{
DrawPropertiesExcluding(serializedObject, nameof(SceneLoader.sceneName));
var scene = serializedObject.FindProperty(nameof(SceneLoader.scene));
var sceneName = serializedObject.FindProperty(nameof(SceneLoader.sceneName));
sceneName.stringValue = scene.objectReferenceValue.name;
}
serializedObject.ApplyModifiedProperties();
}
}
#endif
public class SceneLoader : MonoBehaviour
{
#if UNITY_EDITOR
public SceneAsset scene;
#endif
[HideInInspector]
public string sceneName;
public void LoadScene()
{
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
}
}
씬들로드
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(GotoRandomScene), true)]
public class GotoRandomSceneInspecter : Editor
{
public override void OnInspectorGUI()
{
EditorGUI.BeginChangeCheck();
serializedObject.Update();
{
DrawPropertiesExcluding(serializedObject, nameof(GotoRandomScene.sceneNames));
}
serializedObject.ApplyModifiedProperties();
if (EditorGUI.EndChangeCheck())
{
var picker = target as GotoRandomScene;
picker.sceneNames = System.Array.ConvertAll(picker.scenes, x => x.name);
}
}
}
#endif
public class GotoRandomScene : MonoBehaviour
{
[Header("랜덤룸으로 이동함")]
#if UNITY_EDITOR
public UnityEditor.SceneAsset[] scenes;
#endif
public string[] sceneNames;
}
현재 씬 이름
UnityEngine.SceneManagement.SceneManager.GetActiveScene().name
환경은 그대로 두고 맵만 불러올떄 사용한거
var sceneName = gotoRandomScene.sceneNames[sceneNumber];
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
//없는 이름 스킵
if (System.Array.Find(gotoRandomScene.sceneNames, x => x == scene.name) == null)
{
continue;
}
//원하는 씬이 맞음
if (sceneName == scene.name)
{
return;
}
//원하는 씬이 아님
{
SceneManager.UnloadSceneAsync(scene);
}
}
//원하는 씬이 없음
{
DestroyAll();
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
}
/*
if (sceneName != UnityEngine.SceneManagement.SceneManager.GetActiveScene().name)
{
DestroyAll();
UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName, LoadSceneMode.Additive);
}
*/
'Unity > C#' 카테고리의 다른 글
형변환 모음 (0) | 2020.08.11 |
---|---|
C# 파일관련 함수들 (0) | 2020.07.30 |
c# Array(배열)관련 (0) | 2020.07.29 |