Files
Infratest/Assets/Code/Example/Race/Track/WellEditor/CheckPointAsset.cs
T

39 lines
1.3 KiB
C#

namespace Code.Example.Race.Track.WellEditor
{
using UnityEngine;
using UnityEditor;
public static class CheckPointAsset
{
[MenuItem("GameObject/Track/CheckPoint", false, 1)]
private static void CreateCustomPrefab()
{
// Define the path to your prefab in the Project (must be in an "Assets" relative path)
var prefabPath = "Assets/Resources/Example/Misc/Point.prefab"; // Update with your actual path
// Load the prefab asset
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
if (!prefab)
{
Debug.LogError("Prefab not found at path: " + prefabPath);
return;
}
// Instantiate the prefab in the scene
var newInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
// Ensure it is placed correctly in the Hierarchy and is undoable
Undo.RegisterCreatedObjectUndo(newInstance, "Create Custom Prefab");
Selection.activeGameObject = newInstance;
// Set parent if an object is selected
if (!Selection.activeTransform)
{
newInstance.transform.parent = Selection.activeTransform;
newInstance.transform.localPosition = Vector3.zero;
}
}
}
}