Custom Type of scriptableObject

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
Colobo
Posts: 3
Joined: Wed Jul 08, 2015 6:52 pm

Custom Type of scriptableObject

Post by Colobo »

Hello guys!

I am trying to write a custom type for a scriptable object.

Automatically, this is the code generated by ease save 2:

Code: Select all

public override object Read(ES2Reader reader)
    {
        MyScriptableObject data = new MyScriptableObject ();
        // Add your reader.Read calls here and return your object.
        data.isEnabled = reader.Read<System.Boolean>();

        return data;
    }
and then I read it by this:

Code: Select all

 ES2.Load<MyScriptableObject >(myScriptableObject .GetCompletedID());
But this is not working.

So, what are is the best practiques for scriptableObjects?

Thank you very much!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Custom Type of scriptableObject

Post by Joel »

Hi there,

As ScriptableObject can only be created using ScriptableObject.CreateInstance<T>(), you would need to modify the Read method in the ES2Type file to this:
public override object Read(ES2Reader reader)
   {
       MyScriptableObject data = ScriptableObject.CreateInstance<MyScriptableObject>();
       // Add your reader.Read calls here and return your object.
       data.isEnabled = reader.Read<System.Boolean>();

       return data;
   }
We'll see about adding support for ScriptableObject inManage Types in Easy Save v2.6.6 so it automatically does this for you.

All the best,
Joel
Colobo
Posts: 3
Joined: Wed Jul 08, 2015 6:52 pm

Re: Custom Type of scriptableObject

Post by Colobo »

Thanks for the reply!

It doesn't throw any error, but unfortunately it doesn't work.

I am using it like this

Code: Select all

public bool LoadAsset(MyScriptableObject asset)
    {
        string id = asset.GetCompletedID();
        if (ES2.Exists(id))
        {
            asset = ES2.Load<MyScriptableObject >(id);

            return true;
        }
        else
        {
            return false;
        }
    }
I also try this

Code: Select all

ES2.Load<MyScriptableObject >(id, asset);

but it doesnt compile since MyScriptable object is not a Component.

Thank you
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Custom Type of scriptableObject

Post by Joel »

Hi there,

It will only save and load the isEnabled variable of your ScriptableObject, because that's the only variable you've told the ES2Type to save.

If you want to save other variables, you'll need to tick them in Manage Types (if they're supportable), or manually add the Read and Write methods as per the instructions here.

If you paste your ScriptableObject class here, and let me know what needs to be saved, I'll see if I can assist with this.

All the best,
Joel
Colobo
Posts: 3
Joined: Wed Jul 08, 2015 6:52 pm

Re: Custom Type of scriptableObject

Post by Colobo »

Thank you and sorry for the delay.

I have done a workaround with the boolean and its work, but with the custom type don't. Here is my tests

ScriptableObject

Code: Select all

using UnityEngine;
using System.Collections;
using System;

[Serializable]
public class DickyAsset:ScriptableObject,IComparable  {

    public enum AssetType
    {
        GLASSES,
        HAT,
        BODY,
    }
    
    public AssetType assetType;
    public int id;


	public Sprite sprite;
	public Sprite smallPreview;
	public int prize;
	public bool isEnabled;
	public Vector3 offSet;

    public string GetCompletedID()
    {
        return id.ToString("000") + "@" + assetType.ToString();
    }

    int IComparable.CompareTo(object obj)
	{
		DickyAsset asset = (DickyAsset)obj;
		return String.Compare(this.sprite.name,asset.sprite.name);
		
	}
}
Custom Type

Code: Select all

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ES2UserType_DickyAsset : ES2Type
{
	public override void Write(object obj, ES2Writer writer)
	{
		DickyAsset data = (DickyAsset)obj;
		// Add your writer.Write calls here.
		writer.Write(data.isEnabled);

	}
	
	public override object Read(ES2Reader reader)
	{
		DickyAsset data = ScriptableObject.CreateInstance<DickyAsset>();
		// Add your reader.Read calls here and return your object.
		data.isEnabled = reader.Read<System.Boolean>();

		return data;
	}
	
	/* ! Don't modify anything below this line ! */
	public ES2UserType_DickyAsset():base(typeof(DickyAsset)){}
}
Use

Code: Select all

if (GUILayout.Button ("Save")) {
			ES2.Save<DickyAsset>(dickyAsset, dickyAsset.GetCompletedID());
		}

		if (GUILayout.Button ("Load")) {

			string id = dickyAsset.GetCompletedID();

			if (ES2.Exists(id))
			{
				dickyAsset = ES2.Load<DickyAsset>(dickyAsset.GetCompletedID());
			}
			else
			{
				Debug.Log("Do not exists");
			}
		}
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Custom Type of scriptableObject

Post by Joel »

Hi there,

You need to add the Write and Read calls in the ES2Type for the other properties of your DickyAsset class. i.e.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class ES2UserType_DickyAsset : ES2Type
{
   public override void Write(object obj, ES2Writer writer)
   {
      DickyAsset data = (DickyAsset)obj;
      // Add your writer.Write calls here.
      writer.Write(data.assetType);
      writer.Write(data.id);
      writer.Write(data.sprite);
      writer.Write(data.smallPreview);
      writer.Write(data.prize);
      writer.Write(data.isEnabled);
      writer.Write(data.offSet);
   }
   
   public override object Read(ES2Reader reader)
   {
      DickyAsset data = ScriptableObject.CreateInstance<DickyAsset>();
      // Add your reader.Read calls here and return your object.
      data.assetType = reader.Read<AssetType>();
      data.id = reader.Read<int>();
      data.sprite = reader.Read<Sprite>();
      data.smallPreview = reader.Read<Sprite>();
      data.prize = reader.Read<int>();
      data.isEnabled = reader.Read<bool>();
      data.offset = reader.Read<Vector3>();

      return data;
   }
   
   /* ! Don't modify anything below this line ! */
   public ES2UserType_DickyAsset():base(typeof(DickyAsset)){}
}
All the best,
Joel
_eka_
Posts: 1
Joined: Wed Dec 20, 2017 10:30 am

Re: Custom Type of scriptableObject

Post by _eka_ »

Joel wrote:
We'll see about adding support for ScriptableObject inManage Types in Easy Save v2.6.6 so it automatically does this for you.

All the best,
Joel
Hi, I was wondering if this was already implemented, didn't found it in the supported types tho.

Thanks
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Custom Type of scriptableObject

Post by Joel »

Hi there,

ScriptableObjects are supported, you will just need to select what variables of the ScriptableObject you want to save in the Assets > Easy Save 2 > Manage Types window.

For more information, see this guide: http://docs.moodkie.com/easy-save-2/gui ... for-a-type

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Locked