是否可以使用可用的 AssetDatabase 来构建游戏?

问题描述 投票:0回答:1

我使用 AssetDatabase 制作关卡编辑器。每个对象都保存在单独的资产(文件)中。但不幸的是,在 Unity 中我们无法使用 AssetDatabase 构建游戏

(除了可以将带有AssetDatabase的脚本添加到Editor文件夹中,或者指定#if UNITY_EDITOR,但它在组装的游戏中不起作用,所以这个选项不适合我)。

因此,我向您寻求帮助:也许有类似AssetDatabase的东西可以用来构建游戏,或者Unity Asset Store中有任何工具

using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering.Universal;
using DG.Tweening;

[CreateAssetMenu(fileName = "Level Object", menuName = "Level Object")]
public class ObjectProperty : ScriptableObject
{
    [Header("Main")]
    public string objectName;
    public string shape = "Ball";
    public Vector2 position = Vector2.zero;
    public float rotation = 0;
    public int sortingOrder;
    public Color color = Color.white;
    public float opacity = 1f;
    public bool glow = false;
    public float glowIntensity = .2f;
    public float glowRadius = 1f;
    public float glowSmoothness = .5f;

    [Header("Extended")]
    public float spawnDelay = 0f;
    public float movingSpeed = 5f;
    public float rotationSpeed = 1f;
    public Ease easing = Ease.InOutCubic;

    public static int number = 1;
    public static string assetPath = "Assets/Scripts/LevelEditor/Object/ObjectSaving/LevelTest";

    public static GameObject CreateObjectByAsset(ObjectProperty data) // Reads asset file at the given path and spawns the GameObject from the file data
    {
        GameObject spawnedObject = Instantiate(Resources.Load<GameObject>($"Objects/EditorPrefabs/{data.shape}"), data.position, Quaternion.Euler(0f, 0f, data.rotation));
        SpriteRenderer renderer = spawnedObject.GetComponent<SpriteRenderer>();
        Light2D glow = spawnedObject.GetComponent<Light2D>();

        spawnedObject.name = data.objectName;

        renderer.color = new(data.color.r, data.color.g, data.color.b, data.opacity);
        renderer.sortingOrder = data.sortingOrder;

        glow.enabled = data.glow;
        glow.color = renderer.color;
        glow.intensity = data.glowIntensity;
        glow.pointLightOuterRadius = data.glowRadius;
        glow.falloffIntensity = data.glowSmoothness;

        return spawnedObject;
    }
    public static ObjectProperty SaveGameObjectInAsset(GameObject obj) // Saves the GameObject to an asset file with return ability
    {
        SpriteRenderer renderer = obj.GetComponent<SpriteRenderer>();
        Light2D glow = obj.GetComponent<Light2D>();

        if (Directory.GetFiles(assetPath).Length == 0)
            number = 1;

        while (File.Exists($"{assetPath}/{number}.asset"))
            number++;

        ObjectProperty data = CreateInstance<ObjectProperty>();
            data.objectName = obj.name;
            data.shape = renderer.sprite.name;
            data.position = obj.transform.position;
            data.rotation = obj.transform.rotation.eulerAngles.z;
            data.sortingOrder = renderer.sortingOrder;
            data.color = new(renderer.color.r, renderer.color.g, renderer.color.b);
            data.opacity = renderer.color.a;
            data.glow = glow.enabled;
            data.glowIntensity = glow.intensity;
            data.glowRadius = glow.pointLightOuterRadius;
            data.glowSmoothness = glow.falloffIntensity;

        AssetDatabase.CreateAsset(data, $"{assetPath}/{number}.asset");
        ObjectProperty asset = AssetDatabase.LoadAssetAtPath<ObjectProperty>($"{assetPath}/{number}.asset");

        return asset;
    }
    public static ObjectProperty FindAssetByNumber(int number) // Finds an asset file by number: Obj1, Obj2, Obj3...
    {
        ObjectProperty data = AssetDatabase.LoadAssetAtPath<ObjectProperty>($"{assetPath}/{number}.asset");
        return data;
    }
    public static ObjectProperty[] GetObjectProperties()
    {
        string[] guids = AssetDatabase.FindAssets($"t:{nameof(ObjectProperty)}", new[] {ObjectProperty.assetPath});
        ObjectProperty[] datas = new ObjectProperty[guids.Length];

        for (int i = 0; i < guids.Length; i++)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
            datas[i] = AssetDatabase.LoadAssetAtPath<ObjectProperty>(assetPath);
        }

        return datas;
    }
    public static void DeleteAsset(int number) // Finds an ObjectProperty asset and deletes it
    {
        AssetDatabase.DeleteAsset($"{assetPath}/{number}.asset");
    }

    public static void ClearAllAssets() // Clears all the assets in the ObjectProperty assets directory and resets the number
    {
        string[] files = Directory.GetFiles(assetPath);

        foreach (string file in files)
            File.Delete(file);

        number = 1; 
        PlayerPrefs.SetInt("number", number);
        AssetDatabase.Refresh();
        PlayerPrefs.Save();
    }

    public static void SaveAssets() =>
        AssetDatabase.SaveAssets();
}

我已经尝试使用 JSON 保存文件 3 次,但没有成功 - 这对我来说太困难了。

构建游戏时,我收到错误“当前上下文中不存在名称‘AssetDatabase’”

unity-game-engine
1个回答
0
投票

您不能使用

AssetDatabase
或放置在
UnityEditor
命名空间内的任何其他实体。我相信,您可以使用的最接近
AssetDatabase
功能是资源

您还可以检查Streaming Assets,它可以在大多数平台上仅与常规文件系统一起使用。但在某些平台上您可能会遇到一些隐藏的困难,例如 Android。

更先进(但也更困难)的资产管理方法是Addressables。但我建议您在不了解它的工作原理以及为什么需要它的情况下,不要迁移到 Addressables。

© www.soinside.com 2019 - 2024. All rights reserved.