Interface, Abstract class when to use at game programming?

I have 2 questions.

So first, there are many example of how to write interface, but they are mostly for Visual studio environment, so using void Main. and seems a bit different with unity environment.

I found this example(c# - Interfaces — What's the point? - Stack Overflow), but doesn’t work at unity.

interface ICreature
{
    void Walk(int distance)
}

public class Troll : ICreature
public class Orc : ICreature

//etc
Front end is then:

void SpawnCreature(creatureType)
{
    ICreature creature;

    switch(creatureType)
    {
         case Orc:

           creature = new Orc();

          case Troll:

            creature = new Troll();
    }

    creature.Walk();
}

But I tried to redo this in unity like this, but error occurs,
“Assets/Scripts/Interface2.cs(27,17): error CS0151: A switch expression of type `ICreature’ cannot be converted to an integral type, bool, char, string, enum or nullable type”

using UnityEngine;
using System.Collections;

interface ICreature{
    void Walk(int distance);
}

public class Troll : ICreature{
    public void Walk(int dis){
        Debug.Log("troll walk");
    }
}
public class Orcmon : ICreature{
    public void Walk(int dis){
        Debug.Log("orc walk");
    }
}

public class Interface2 : MonoBehaviour {
    ICreature a;
    void Start () {
        Troll a = new Troll();
        Dofunc(a);
    }
    ICreature cr;
    void Dofunc(ICreature a){
        switch(a){
        case Troll:
            cr = new Troll();
        case Orcmon:
            cr = new Orcmon();
        }
        cr.Walk(1);
    }
}

Then how should I revise?

So are there good explanation, example of usage of interface, abstract class? in real example when doing game programming by Unity. So when to use? Concrete example is appreciated.

I use them all the time. With a component based system like Unity you can use interfaces between components to give greater modularity.

Interfaces are great for insuring objects can handle what you expect them to, but aren’t necessarily similar enough to warrant inheritance. That’s really it though. Part of my issue I had with interfaces was learning to accept that there’s nothing magical about them. They’re literally just contracts to enforce implementing classes utilize the declared methods and properties. The end, haha.

As far as abstract classes go, I don’t much bother with them. They’re kind of a hybrid between a full class and an interface and I’ve found that either full inheritance or an interfaces works well enough for me. I wouldn’t worry about it. If you run into a situation where you’re creating a lot of similar objects that may or may not need full functionality of an inherited object I’d start investing abstract classes.

Finally, I searched and googled, did like this, and this works in unity. Solution to my first question.

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

interface ICreature{
    void Walk(int distance);
}
interface ICanTurnToStone{
    void TurnToStone();
}
public class Troll : ICreature, ICanTurnToStone{
    public void Walk(int dis){
        Debug.Log("troll walk");
    }
    public void TurnToStone(){
        Debug.Log("turned to stone");
    }
}
public class Orcmon : ICreature{
    public void Walk(int dis){
        Debug.Log("orc walk");
    }
}

public class Interface2 : MonoBehaviour {
    void Start () { 
        Imple();
    }

    public void Imple(){
        List<ICreature> list = new List<ICreature>();
        Troll tr = new Troll();
        Orcmon orc = new Orcmon();
        list.Add(tr);
        list.Add(orc);
      
        foreach(var cr in list){
            cr.Walk(1);
        }
        foreach(var cr in list.OfType<ICanTurnToStone>()){
            cr.TurnToStone();
        }
    }
}

Well, let me take a stab at your #2 then – interfaces are good for 2 main reasons:

  • Polymorphism. The example you have above is using interfaces for this purpose. You can have a collection of objects all adhering to the same interface, and treat them identically without knowing what they actually are.
  • Loose-coupling. Coupling refers to a dependency between two pieces of code. The more dependencies a piece of code has, the harder it becomes to change it. One example the StrangeIOC guys gave was having your code tied to some kind of social network – maybe you put a bunch of Facebook calls in your code. Then, when your boss tells you he’d prefer to use Twitter instead of Facebook, you need to make major changes. Interfaces would have instead allowed you to define a common set of operations a “Social Network” would support, and then not really care whether you’re using Facebook or Twitter.