Author Topic: Game Logic and UI  (Read 2894 times)

lzt120

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 68
    • View Profile
Game Logic and UI
« on: January 17, 2013, 03:42:18 AM »
I am new to programming.My question is that should UI code and Game Logic code put into two seperate files and use the static method for the logic. UI Code just call that method's return value. for example:

Logic code :

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

   public static string GetStr(float i)
   {
      return i.ToString("f2");
   }
}


UI code:

using UnityEngine;
using System.Collections;

public class TestDisplay : MonoBehaviour {

   public UILabel lable;
   
   void Update ()
   {
      lable.text = Test.GetStr(Time.time);
   }
}

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Game Logic and UI
« Reply #1 on: January 17, 2013, 04:44:40 AM »
Not necessarily. Unity tends to couple view and game pretty closely, but you can do it any way you want. You can do it statically (no reason to have that as a monobehavior though), as a singleton, in components. Many ways to do it.

Having it statically will ensure that you only have a single value to read from, which is good for things like score, coins etc.