// zlib/libpng License // // Copyright (c) 2018 Sinoa // // This software is provided 'as-is', without any express or implied warranty. // In no event will the authors be held liable for any damages arising from the use of this software. // Permission is granted to anyone to use this software for any purpose, // including commercial applications, and to alter it and redistribute it freely, // subject to the following restrictions: // // 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. // If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. // 3. This notice may not be removed or altered from any source distribution. using UnityEngine; using UnityObject = UnityEngine.Object; namespace IceMilkTea.Core { /// /// Unity関連実装でユーティリティな関数として使えるような、関数が実装されているクラスです /// public static class ImtUnityUtility { /// /// 永続的に存在し続けるゲームオブジェクトを生成します。 /// この関数で生成されるゲームオブジェクトはヒエラルキに表示されません。 /// また、名前はNewGameObjectとして作られます。 /// /// 生成された永続ゲームオブジェクトを返します public static GameObject CreatePersistentGameObject() { // "NewGameObject" な見えないゲームオブジェクトを生成して返す return CreatePersistentGameObject("NewGameObject", HideFlags.HideInHierarchy); } /// /// 永続的に存在し続けるゲームオブジェクトを生成します。 /// この関数で生成されるゲームオブジェクトはヒエラルキに表示されません。 /// /// 生成する永続ゲームオブジェクトの名前 /// 生成された永続ゲームオブジェクトを返します public static GameObject CreatePersistentGameObject(string name) { // 見えないゲームオブジェクトを生成して返す return CreatePersistentGameObject(name, HideFlags.HideInHierarchy); } /// /// 永続的に存在し続けるゲームオブジェクトを生成します。 /// /// 生成する永続ゲームオブジェクトの名前 /// 生成する永続ゲームオブジェクトの隠しフラグ /// 生成された永続ゲームオブジェクトを返します public static GameObject CreatePersistentGameObject(string name, HideFlags hideFlags) { // ゲームオブジェクトを生成する var gameObject = new GameObject(name); // ヒエラルキから姿を消して永続化 gameObject.hideFlags = hideFlags; UnityObject.DontDestroyOnLoad(gameObject); // トランスフォームを取得して念の為初期値を入れる var transform = gameObject.GetComponent(); transform.position = Vector3.zero; transform.rotation = Quaternion.identity; transform.localScale = Vector3.one; // 作ったゲームオブジェクトを返す return gameObject; } } }