1

I am trying to build out some content for a strategy game I'm building in Unity3d. I am currently using Scriptable Objects to create assets, however I'm having an issue getting exactly what I would like when it comes to linking the assets.

Let me give a proper example with some code. Lets say I have a building that accepts resources. So I might have my building SCO to look like:

public class buildingSCO : ScriptableObject {
    public string Name;
    public string ID;
    public string Res;
}

And my resource SCO would look like:

public class resourceSCO : ScriptableObject {
    public string Name;
    public string ID;
}

So when I create the asset for the building I would enter the ID of the resource by hand (or copy/paste). But my concern is that as I create more and more assets I'll start to make errors. What I'd like is to be able to open an object selector and just select the resource asset I want the id of.

1
  • is there a chnce you could edit and shorten your question? eg totally eliminate the first 3 sentences Commented Jan 23, 2016 at 1:34

1 Answer 1

1

I don't think Unity will create copies and copies of your SCOs. Just reference them the way Unity intends you to, and you will be fine.

public class buildingSCO : ScriptableObject
{
    public string Name;
    public string ID;
    public resourceSco Res;
}

First rule of optimization: don't optimize.

If you really, really need to work with Ids and lookups, you can do that by creating a Custom Inspector.

[CustomEditor(typeof(buildingSCO))]
public class BuildingSCOInspector : Editor
{
    public override void OnInspectorGUI()
    {
        buildingSCO building = target as buildingSCO;

        building.Name = EditorGUILayout.TextField("Name", building.Name);
        building.ID = EditorGUILayout.TextField("ID", building.ID); // or whatever

        // this only shows currently selected resourceSCO.ID
        EditorGUILayout.LabelField("Res (current)", building.Res);

        // this allows selection of another resourceSCO
        resourceSCO resource = EditorGUILayout.ObjectField("Res", null, typeof(resourceSCO), false) as resourceSCO;
        if (resource != null)
        {
            building.Res = resource.ID;
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am going to check your custom editor, I think it might be what I was looking for. I just re-read my question and I can see where you thought I was concerned about the multiple copies. I will try to clear the question up.
The custom editor is exactly what I was looking for. There were some issues with code (like the OnInspectorGUI signature needs to be public override), so I'll make a quick edit to what you posted. This will help me ensure I don't enter an Id incorrectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.