BattlehubCode / RTE_Docs

This is a repository for Runtime Editor documentation, discussion and tracking features and issues.
https://assetstore.unity.com/packages/tools/modeling/runtime-editor-64806
11 stars 1 forks source link

[AssetDatabase] Sprite is not saved #110

Closed BattlehubCode closed 5 months ago

BattlehubCode commented 5 months ago

I have used custom class in AssetDatabse, Sprite object not saved in surrogates database after saving scene, spirte object missing in Class model

image

BattlehubCode commented 5 months ago

I tested it using the following script and it seems to work fine. Make sure you are returning a Sprite from the Enumerator class.

using UnityEngine;

public class ScriptWithSpriteProperty : MonoBehaviour
{
    [SerializeField]
    private Sprite m_sprite;

    public Sprite Sprite
    {
        get { return m_sprite; }
        set { m_sprite = value; }
    }

    private void Start()
    {
        Debug.Log(Sprite.name);
        Debug.Log(Sprite.texture.name);
        Debug.Log(Sprite.texture.GetPixel(1, 1));
    }
}
using Battlehub.RTCommon;
using Battlehub.RTEditor;
using UnityEngine;

public class ScriptWithSpritePropertyTest : MonoBehaviour
{
    void Start()
    {
        GameObject go = new GameObject("ScriptWithSpriteProperty");
        var script = go.AddComponent<ScriptWithSpriteProperty>();

        Texture2D texture = new Texture2D(32, 32);
        texture.SetPixel(1, 1, Color.green);
        texture.Apply();

        script.Sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);

        IOC.Resolve<IRuntimeEditor>().AddGameObjectToScene(go);
    }
}

This is generated surrogate code:

using ProtoBuf;
using System;
using System.Threading.Tasks;

namespace Battlehub.Storage.Surrogates
{
    [ProtoContract]
    [Surrogate(typeof(global::ScriptWithSpriteProperty), _PROPERTY_INDEX, _TYPE_INDEX)]
    public class ScriptWithSpritePropertySurrogate<TID> : ISurrogate<TID> where TID : IEquatable<TID>
    {   
        const int _PROPERTY_INDEX = 5;
        const int _TYPE_INDEX = 4153;

        //_PLACEHOLDER_FOR_EXTENSIONS_DO_NOT_DELETE_OR_CHANGE_THIS_LINE_PLEASE

        [ProtoMember(2)]
        public TID id { get; set; }

        [ProtoMember(3)]
        public TID gameObjectId { get; set; }

        [ProtoMember(4)]
        public TID Sprite { get; set; }

        [ProtoMember(5)]
        public global::System.Boolean enabled { get; set; }

        //_PLACEHOLDER_FOR_NEW_PROPERTIES_DO_NOT_DELETE_OR_CHANGE_THIS_LINE_PLEASE

        public ValueTask Serialize(object obj, ISerializationContext<TID> ctx)
        {
            var idmap = ctx.IDMap;

            var o = (global::ScriptWithSpriteProperty)obj;
            id = idmap.GetOrCreateID(o);
            gameObjectId = idmap.GetOrCreateID(o.gameObject);
            Sprite = idmap.GetOrCreateID(o.Sprite);
            enabled = o.enabled;
            //_PLACEHOLDER_FOR_SERIALIZE_METHOD_BODY_DO_NOT_DELETE_OR_CHANGE_THIS_LINE_PLEASE

            return default;
        }

        public ValueTask<object> Deserialize(ISerializationContext<TID> ctx)
        {
            var idmap = ctx.IDMap;

            var o = idmap.GetComponent<global::ScriptWithSpriteProperty, TID>(id, gameObjectId);
            o.Sprite = idmap.GetObject<global::UnityEngine.Sprite>(Sprite);
            o.enabled = enabled;
            //_PLACEHOLDER_FOR_DESERIALIZE_METHOD_BODY_DO_NOT_DELETE_OR_CHANGE_THIS_LINE_PLEASE

            return new ValueTask<object>(o);
        }
    }
}

And this is generated enumerator code:

namespace Battlehub.Storage.Enumerators
{
   [ObjectEnumerator(typeof(global::ScriptWithSpriteProperty))]
    public class ScriptWithSpritePropertyEnumerator : ObjectEnumerator<global::ScriptWithSpriteProperty>
    {
        public override bool MoveNext()
        {
            do
            {
                switch (Index)
                {

                    case 0:
                        if (MoveNext(TypedObject.Sprite, 3))
                            return true;
                        break;
                    case 1:
                        if (MoveNext(Object, -1))
                            return true;
                        break;
                    default:
                        return false;
                }
            }
            while (true);
        }
    }
}

Alternative enumerator syntax:

using System.Collections.Generic;

namespace Battlehub.Storage.Enumerators
{
    [ObjectEnumerator(typeof(global::ScriptWithSpriteProperty))]
    public class ScriptWithSpritePropertyEnumerator : ObjectEnumerator<global::ScriptWithSpriteProperty>
    {
        protected override IEnumerator<(object Object, int Key)> GetNext()
        {
            yield return (TypedObject.Sprite, 3);
            yield return (TypedObject, -1);
        }
    }
}