Files
2026-05-26 16:15:54 +08:00

174 lines
4.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using cfg;
using Newtonsoft.Json;
using SimpleJSON;
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode]
[System.Serializable]
public class CubeScript : MonoBehaviour
{
#if UNITY_EDITOR
public int PropId = 0;
[AngleSelector]
public int Angle = 0;
public bool IsBorder = false;
public bool DoubleClick = false;
public int ID = 1;
[HideInInspector]
public Vector3 LocalPos = Vector3.zero;
[HideInInspector]
public CubeConfigData ConfigData;
private int _oidAngle = 0;
private int _oidPropId = 0;
private GameObject currentInstance;
private Tables _tables;
private Tables GetTable()
{
if (_tables == null)
{
_tables = new Tables((string fileName) =>
{
return JSON.Parse(
File.ReadAllText(Application.dataPath + "/ABPackage/pkgFirstPackage/Data/" + fileName + ".json"));
});
}
return _tables;
}
private void Awake()
{
_oidAngle = Angle;
if (PropId > 0)
{
ApplyId();
}
}
void OnValidate()
{
if (PropId != _oidPropId)
{
_oidPropId = PropId;
//EditorApplication.delayCall += UpdateModel;
}
if (Angle != _oidAngle)
{
_oidAngle = Angle;
if (ConfigData != null)
{
ConfigData.PropDirection = _oidAngle;
}
EditorApplication.delayCall += UpdateAngle;
}
}
public void ApplyId()
{
UpdateModel();
}
private void UpdateAngle()
{
if (currentInstance != null && ConfigData != null)
{
ConfigData.PropDirection = _oidAngle;
currentInstance.transform.localEulerAngles = new Vector3(0, 0, -ConfigData.PropDirection);
}
}
private void UpdateModel()
{
if (PropId <= 0)
{
_oidAngle = 0;
Angle = 0;
ConfigData = null;
return;
}
string templateName = GetTable().TbDiggingItemList.Get(PropId).GridTemplate;
string templateUrl = $"Assets/AssetsPackage/art/DgSand/Scriptable/CubeConfig/{templateName}.asset";
CubeConfigData asset = AssetDatabase.LoadAssetAtPath<CubeConfigData>(templateUrl);
if (asset == null)
{
Debug.LogError("格子上的道具布局信息 没找到,请检查配置!!" + PropId);
return;
}
ConfigData = asset.Copy();
ConfigData.PropDirection = _oidAngle;
// 清空所有子物体
//foreach (Transform child in transform)
//{
// DestroyImmediate(child.gameObject);
//}
currentInstance = null;
if (!GetTable().TbDiggingItemList.DataMap.TryGetValue(PropId, out DiggingItemList info))
{
return;
}
string modelName = GetTable().TbDiggingItemList.Get(PropId).ItemGridResource;
Debug.LogWarning("目标道具资源名字:" + modelName + " x:" + LocalPos.x + " y:" + LocalPos.y + " angle:" + ConfigData.PropDirection);
string modelUrl = $"Assets/ABPackage/pkgDownloads/EventDigging/DgSand/GridProp/DgSand{modelName}.prefab";
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(modelUrl);
if (prefab != null)
{
prefab.transform.localPosition = new Vector3(0, 0, -0.5f);
prefab.transform.localEulerAngles = new Vector3(0, 0, -ConfigData.PropDirection);
currentInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefab, transform);
}
else
{
Debug.LogWarning("Prefab not found at path: " + modelUrl);
}
}
public void Update()
{
LocalPos = this.transform.localPosition;
}
public string ToJson()
{
if (ConfigData != null && PropId <= 0)
{
Debug.LogError("检查资源 PropId 没有设置!!!" + " x:" + LocalPos.x + " y:" + LocalPos.y);
return String.Empty;
}
if (PropId > 0 && ConfigData == null)
{
EditorUtility.DisplayDialog("格子道具配置数据没有生成 ConfigData", "PropId " + PropId + " 位置:" + " x:" + LocalPos.x + " y:" + LocalPos.y, "OK");
return String.Empty;
}
var serializablePos = new SerializableVector3(LocalPos);
return JsonConvert.SerializeObject(new { IsBorder = IsBorder, DoubleClick = DoubleClick, PropId = PropId, ConfigData = ConfigData, LocalPos = serializablePos, ID = ID });
}
#endif
}