using UnityEngine; using System.Runtime.InteropServices; public class BaseTextureManager : MonoBehaviour { [System.Serializable] public class TextureUpdateData { public string base64Data; public float tiling_x; public float tiling_y; } // 引用地板的材质 public Material floorMaterial; // // 声明JavaScript函数,用于调试 // [DllImport("__Internal")] // private static extern void DebugToConsole(string message); // 用于接收和处理base64图片数据的方法 public void UpdateTexture(string jsonData) { try { // 解析 JSON 数据 TextureUpdateData data = JsonUtility.FromJson(jsonData); Debug.Log($"UpdateTexture: {data.base64Data}"); Debug.Log($"floorMaterial: {floorMaterial.name}"); // 移除base64数据的header string pureBase64 = data.base64Data.Substring(data.base64Data.IndexOf(",") + 1); // 将base64转换为字节数组 byte[] imageBytes = System.Convert.FromBase64String(pureBase64); // 创建新的贴图 Texture2D texture = new Texture2D(2, 2); texture.LoadImage(imageBytes); // 设置贴图的包裹模式 texture.wrapMode = TextureWrapMode.Repeat; // 更新材质的主贴图 floorMaterial.mainTexture = texture; // 设置贴图的平铺值 floorMaterial.mainTextureScale = new Vector2(data.tiling_x, data.tiling_y); } catch (System.Exception e) { Debug.Log($"UpdateTexture Error: {e.Message}"); // #if UNITY_WEBGL && !UNITY_EDITOR // DebugToConsole("Error updating texture: " + e.Message); // #endif } } }