Skip to main content
Minor question formatting, markdown formatting
Source Link
liggiorgio
  • 5.5k
  • 6
  • 27
  • 38

I'm extremely new to Unity, and I'm trying to figure out if this design makes sense. I'm creating a card game in which multiple instances of the same card will exist. I'm trying to determine if the "idiomatic" way to do this is as follows:

  • Create a Scriptable Object to contain card metadata.
  • Then create specific Scriptable Objects for each card.
  • Then at runtime, whenever I need a specific card, I create a gameObject add the (simple for example purposes) IsCard MonoBehavior and associate IsCard with whatever ScriptableObject corresponds to the specific card in question to extract/assign metadata.

Does this make sense or is there a more performant/idiomatic way to do this?

This is my current implementation of the above solution.

using UnityEngine;
using UnityEngine.UI;
 
 
[CreateAssetMenu(fileName = "CardSO", menuName = "ScriptableObjects/Card")]
public class CardSO : ScriptableObject
{
    public string cardName;
    public Image cardImage;
    // There could be more specific SOs for creatures, spells, etc.
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
[RequireComponent(typeof(Image))]
public class IsCard : MonoBehaviour
{
 
    public CardSO cardSO;
 
    // Start is called before the first frame update
    void Start()
    {
        if (cardSO == null)
        {
            return;
        }
 
        Image image = GetComponent<Image>();
        image.sprite = cardSO.cardImage.sprite;
        //Reference/Utilize other properties when applicable.
        //If this were a creature, perhaps we could have a mono behavior that stores power/toughness, etc.
        //public int power;
        //power = creatureCardSO.power;
    }
}
```

I'm extremely new to Unity, and I'm trying to figure out if this design makes sense. I'm creating a card game in which multiple instances of the same card will exist. I'm trying to determine if the "idiomatic" way to do this is as follows:

Create a Scriptable Object to contain card metadata. Then create specific Scriptable Objects for each card. Then at runtime, whenever I need a specific card, I create a gameObject add the (simple for example purposes) IsCard Mono Behavior and associate IsCard with whatever ScriptableObject corresponds to the specific card in question to extract/assign metadata.

Does this make sense or is there a more performant/idiomatic way to do this?

using UnityEngine;
using UnityEngine.UI;
 
 
[CreateAssetMenu(fileName = "CardSO", menuName = "ScriptableObjects/Card")]
public class CardSO : ScriptableObject
{
    public string cardName;
    public Image cardImage;
    // There could be more specific SOs for creatures, spells, etc.
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
[RequireComponent(typeof(Image))]
public class IsCard : MonoBehaviour
{
 
    public CardSO cardSO;
 
    // Start is called before the first frame update
    void Start()
    {
        if (cardSO == null)
        {
            return;
        }
 
        Image image = GetComponent<Image>();
        image.sprite = cardSO.cardImage.sprite;
        //Reference/Utilize other properties when applicable.
        //If this were a creature, perhaps we could have a mono behavior that stores power/toughness, etc.
        //public int power;
        //power = creatureCardSO.power;
    }
}

I'm extremely new to Unity, and I'm trying to figure out if this design makes sense. I'm creating a card game in which multiple instances of the same card will exist. I'm trying to determine if the "idiomatic" way to do this is as follows:

Create a Scriptable Object to contain card metadata. Then create specific Scriptable Objects for each card. Then at runtime, whenever I need a specific card, I create a gameObject add the (simple for example purposes) IsCard Mono Behavior and associate IsCard with whatever ScriptableObject corresponds to the specific card in question to extract/assign metadata.

Does this make sense or is there a more performant/idiomatic way to do this?

I'm extremely new to Unity, and I'm trying to figure out if this design makes sense. I'm creating a card game in which multiple instances of the same card will exist. I'm trying to determine if the "idiomatic" way to do this is as follows:

  • Create a Scriptable Object to contain card metadata.
  • Then create specific Scriptable Objects for each card.
  • Then at runtime, whenever I need a specific card, I create a gameObject add the (simple for example purposes) IsCard MonoBehavior and associate IsCard with whatever ScriptableObject corresponds to the specific card in question to extract/assign metadata.

Does this make sense or is there a more performant/idiomatic way to do this?

This is my current implementation of the above solution.

using UnityEngine;
using UnityEngine.UI;
 
 
[CreateAssetMenu(fileName = "CardSO", menuName = "ScriptableObjects/Card")]
public class CardSO : ScriptableObject
{
    public string cardName;
    public Image cardImage;
    // There could be more specific SOs for creatures, spells, etc.
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
[RequireComponent(typeof(Image))]
public class IsCard : MonoBehaviour
{
 
    public CardSO cardSO;
 
    // Start is called before the first frame update
    void Start()
    {
        if (cardSO == null)
        {
            return;
        }
 
        Image image = GetComponent<Image>();
        image.sprite = cardSO.cardImage.sprite;
        //Reference/Utilize other properties when applicable.
        //If this were a creature, perhaps we could have a mono behavior that stores power/toughness, etc.
        //public int power;
        //power = creatureCardSO.power;
    }
}
```
Source Link

Idiomatic Way to Create Card Instances in Unity

using UnityEngine;
using UnityEngine.UI;
 
 
[CreateAssetMenu(fileName = "CardSO", menuName = "ScriptableObjects/Card")]
public class CardSO : ScriptableObject
{
    public string cardName;
    public Image cardImage;
    // There could be more specific SOs for creatures, spells, etc.
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
[RequireComponent(typeof(Image))]
public class IsCard : MonoBehaviour
{
 
    public CardSO cardSO;
 
    // Start is called before the first frame update
    void Start()
    {
        if (cardSO == null)
        {
            return;
        }
 
        Image image = GetComponent<Image>();
        image.sprite = cardSO.cardImage.sprite;
        //Reference/Utilize other properties when applicable.
        //If this were a creature, perhaps we could have a mono behavior that stores power/toughness, etc.
        //public int power;
        //power = creatureCardSO.power;
    }
}

I'm extremely new to Unity, and I'm trying to figure out if this design makes sense. I'm creating a card game in which multiple instances of the same card will exist. I'm trying to determine if the "idiomatic" way to do this is as follows:

Create a Scriptable Object to contain card metadata. Then create specific Scriptable Objects for each card. Then at runtime, whenever I need a specific card, I create a gameObject add the (simple for example purposes) IsCard Mono Behavior and associate IsCard with whatever ScriptableObject corresponds to the specific card in question to extract/assign metadata.

Does this make sense or is there a more performant/idiomatic way to do this?