Initial commit
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
|
||||
public class BlockDataCreatorEditor : EditorWindow
|
||||
{
|
||||
private GameObject prefab;
|
||||
private string saveFolder = "Assets/Resources/BlocksData/Final";
|
||||
|
||||
[MenuItem("Tools/Block Data Creator")]
|
||||
public static void ShowWindow() =>
|
||||
GetWindow<BlockDataCreatorEditor>("Block Data Creator");
|
||||
|
||||
private void OnGUI()
|
||||
{
|
||||
GUILayout.Label("Block Data Creator", EditorStyles.boldLabel);
|
||||
EditorGUILayout.Space();
|
||||
|
||||
prefab = (GameObject)EditorGUILayout.ObjectField("Block Prefab", prefab, typeof(GameObject), false);
|
||||
|
||||
EditorGUILayout.Space();
|
||||
GUILayout.Label("Save Paths", EditorStyles.boldLabel);
|
||||
|
||||
EditorGUILayout.BeginHorizontal();
|
||||
saveFolder = EditorGUILayout.TextField("BlockData Folder", saveFolder);
|
||||
if (GUILayout.Button("Browse", GUILayout.Width(60)))
|
||||
saveFolder = BrowseFolder(saveFolder);
|
||||
EditorGUILayout.EndHorizontal();
|
||||
|
||||
EditorGUILayout.Space();
|
||||
|
||||
GUI.enabled = prefab != null;
|
||||
if (GUILayout.Button("Create BlockData"))
|
||||
CreateBlockData();
|
||||
GUI.enabled = true;
|
||||
|
||||
if (prefab == null)
|
||||
EditorGUILayout.HelpBox("Assign a prefab to get started.", MessageType.Info);
|
||||
}
|
||||
|
||||
public void CreateBlockData()
|
||||
{
|
||||
EnsureFolderExists(saveFolder);
|
||||
|
||||
// ── 1. Create BlockData ───────────────────────────────────────────────
|
||||
BlockData blockData = ScriptableObject.CreateInstance<BlockData>();
|
||||
blockData.blockName = prefab.name;
|
||||
blockData.blockPrefab = prefab;
|
||||
|
||||
string assetPath = AssetDatabase.GenerateUniqueAssetPath(
|
||||
$"{saveFolder}/{prefab.name}_BlockData.asset");
|
||||
AssetDatabase.CreateAsset(blockData, assetPath);
|
||||
AssetDatabase.SaveAssets();
|
||||
|
||||
// ── 2. Assign BlockData back to the Block component on the prefab ─────
|
||||
Block block = prefab.GetComponent<Block>();
|
||||
if (block != null)
|
||||
{
|
||||
SerializedObject so = new SerializedObject(block);
|
||||
SerializedProperty prop = so.FindProperty("BlockData");
|
||||
if (prop != null)
|
||||
{
|
||||
prop.objectReferenceValue = blockData;
|
||||
so.ApplyModifiedProperties();
|
||||
PrefabUtility.SavePrefabAsset(prefab);
|
||||
Debug.Log($"[BlockDataCreator] BlockData assigned to {prefab.name}.");
|
||||
}
|
||||
else
|
||||
Debug.LogWarning("[BlockDataCreator] 'BlockData' field not found on Block component.");
|
||||
}
|
||||
else
|
||||
Debug.LogWarning($"[BlockDataCreator] No Block component on {prefab.name}.");
|
||||
|
||||
// ── 3. Ping the new asset ─────────────────────────────────────────────
|
||||
EditorUtility.FocusProjectWindow();
|
||||
Selection.activeObject = blockData;
|
||||
EditorGUIUtility.PingObject(blockData);
|
||||
Debug.Log($"[BlockDataCreator] Created: {assetPath}");
|
||||
}
|
||||
|
||||
public BlockData CreateBlockData(GameObject Prefab)
|
||||
{
|
||||
EnsureFolderExists(saveFolder);
|
||||
|
||||
// ── 1. Create BlockData ───────────────────────────────────────────────
|
||||
BlockData blockData = ScriptableObject.CreateInstance<BlockData>();
|
||||
blockData.blockName = Prefab.name;
|
||||
blockData.blockPrefab = Prefab;
|
||||
|
||||
string assetPath = AssetDatabase.GenerateUniqueAssetPath(
|
||||
$"{saveFolder}/{Prefab.name}_BlockData.asset");
|
||||
AssetDatabase.CreateAsset(blockData, assetPath);
|
||||
AssetDatabase.SaveAssets();
|
||||
|
||||
// ── 2. Assign BlockData back to the Block component on the prefab ─────
|
||||
Block block = Prefab.GetComponent<Block>();
|
||||
if (block != null)
|
||||
{
|
||||
SerializedObject so = new SerializedObject(block);
|
||||
SerializedProperty prop = so.FindProperty("BlockData");
|
||||
if (prop != null)
|
||||
{
|
||||
prop.objectReferenceValue = blockData;
|
||||
so.ApplyModifiedProperties();
|
||||
PrefabUtility.SavePrefabAsset(Prefab);
|
||||
Debug.Log($"[BlockDataCreator] BlockData assigned to {Prefab.name}.");
|
||||
}
|
||||
else
|
||||
Debug.LogWarning("[BlockDataCreator] 'BlockData' field not found on Block component.");
|
||||
}
|
||||
else
|
||||
Debug.LogWarning($"[BlockDataCreator] No Block component on {Prefab.name}.");
|
||||
|
||||
// ── 3. Ping the new asset ─────────────────────────────────────────────
|
||||
EditorUtility.FocusProjectWindow();
|
||||
Selection.activeObject = blockData;
|
||||
EditorGUIUtility.PingObject(blockData);
|
||||
Debug.Log($"[BlockDataCreator] Created: {assetPath}");
|
||||
|
||||
// ── 4. Return the Block Data ──────────────────────────────────────────
|
||||
return blockData;
|
||||
}
|
||||
|
||||
private string BrowseFolder(string current)
|
||||
{
|
||||
string abs = EditorUtility.OpenFolderPanel("Select Folder", current, "");
|
||||
if (string.IsNullOrEmpty(abs)) return current;
|
||||
if (abs.StartsWith(Application.dataPath))
|
||||
return "Assets" + abs.Substring(Application.dataPath.Length);
|
||||
Debug.LogWarning("[BlockDataCreator] Folder must be inside Assets.");
|
||||
return current;
|
||||
}
|
||||
|
||||
private void EnsureFolderExists(string path)
|
||||
{
|
||||
if (AssetDatabase.IsValidFolder(path)) return;
|
||||
string parent = Path.GetDirectoryName(path).Replace("\\", "/");
|
||||
EnsureFolderExists(parent);
|
||||
AssetDatabase.CreateFolder(parent, Path.GetFileName(path));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67e48e5b7a08bb3479888fe7c8bdc644
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,55 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.IO;
|
||||
|
||||
public class BlockDataIconLinker : AssetPostprocessor
|
||||
{
|
||||
// Fires after any assets are imported
|
||||
static void OnPostprocessAllAssets(
|
||||
string[] importedAssets,
|
||||
string[] deletedAssets,
|
||||
string[] movedAssets,
|
||||
string[] movedFromAssetPaths)
|
||||
{
|
||||
foreach (string assetPath in importedAssets)
|
||||
{
|
||||
// Only care about Texture2D PNGs
|
||||
if (!assetPath.EndsWith(".png", System.StringComparison.OrdinalIgnoreCase))
|
||||
continue;
|
||||
|
||||
Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
|
||||
if (texture == null) continue;
|
||||
|
||||
// Try to find a matching BlockData for this texture
|
||||
TryLinkToBlockData(texture, assetPath);
|
||||
}
|
||||
}
|
||||
|
||||
private static void TryLinkToBlockData(Texture2D texture, string texturePath)
|
||||
{
|
||||
string textureName = Path.GetFileNameWithoutExtension(texturePath);
|
||||
|
||||
// Find all BlockData assets in the project
|
||||
string[] guids = AssetDatabase.FindAssets("t:BlockData");
|
||||
foreach (string guid in guids)
|
||||
{
|
||||
string blockDataPath = AssetDatabase.GUIDToAssetPath(guid);
|
||||
BlockData blockData = AssetDatabase.LoadAssetAtPath<BlockData>(blockDataPath);
|
||||
|
||||
if (blockData == null || blockData.blockPrefab == null) continue;
|
||||
|
||||
// Match by checking if the texture name contains the prefab name
|
||||
// Handles prefixes/suffixes like "Icon_zPin", "Icon_zPin_v2", etc.
|
||||
if (!textureName.Contains(blockData.blockPrefab.name)) continue;
|
||||
|
||||
// Skip if already assigned to avoid unnecessary reimports
|
||||
if (blockData.icon == texture || blockData.icon != null) continue;
|
||||
|
||||
blockData.icon = texture;
|
||||
EditorUtility.SetDirty(blockData);
|
||||
AssetDatabase.SaveAssetIfDirty(blockData);
|
||||
|
||||
Debug.Log($"[BlockDataIconLinker] Linked '{texturePath}' → '{blockDataPath}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e135bd5a5f097741b1772c8f5fd8b9e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,51 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public enum PlaneAxis
|
||||
{
|
||||
XZ = 0,
|
||||
XY = 1,
|
||||
YZ = 2
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
public class PlaneSelection
|
||||
{
|
||||
public PlaneAxis planeAxis;
|
||||
public GameObject WorldGrid;
|
||||
}
|
||||
|
||||
public class PlaneSelector : MonoBehaviour
|
||||
{
|
||||
public TMP_Dropdown planeAxisDropDown;
|
||||
|
||||
public PlaneSelection[] PlaneAxis;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
planeAxisDropDown.onValueChanged.AddListener(OnPlaneAxisChanged);
|
||||
UpdatePlaneVisibility();
|
||||
}
|
||||
|
||||
private void OnPlaneAxisChanged(int index)
|
||||
{
|
||||
UpdatePlaneVisibility();
|
||||
}
|
||||
|
||||
private void UpdatePlaneVisibility()
|
||||
{
|
||||
foreach (var plane in PlaneAxis)
|
||||
{
|
||||
if (plane.planeAxis == (PlaneAxis)planeAxisDropDown.value)
|
||||
{
|
||||
plane.WorldGrid.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
plane.WorldGrid.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70c9ce54d0b332b4abe8b68218428d08
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "Scripts.Editor",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"GUID:6055be8ebefd69e48b49212b09b47b2f",
|
||||
"GUID:0f8d877125e73cc40a31b031c2e5fe60"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33db7b02ee883714bad5e572b698bdb7
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user