ゲーム化!tomo_manaのブログ

ゲーム化!tomo-manaのブログ

Unityでゲームを作る方法について紹介しています

Unity×会計#3 現金仕訳 (Unity 2019.4.4f1)

f:id:tomo_mana:20211220005654p:plain

今回から、現金での売買を実装します。

最終的な実装イメージ

商品の売買の仕訳(三分法)、決算ボタンで損益計算書貸借対照表を更新

f:id:tomo_mana:20210813170901p:plain
現金仕訳用のフォーム

今回は、仕入と売上のボタンを作ります(図の赤枠)。

画面設計

f:id:tomo_mana:20210813173419p:plain
入力フォーム Step1

● ボタンは「仕入」と「売上」の2つだけ
●「仕入」で商品を1個仕入れます(@1000)
●「売上」で商品を1個売ります(@1500、利益500)
● 最初の手持ち現金(資本)は10000

Hierarchy

f:id:tomo_mana:20210813185754p:plain
Hierarchy - InputWindow

2つのボタン(ButtonPurchased、ButtonSaled)を追加して、Emptyオブジェクト(JournalButtons)でひとくくりにしています。JournalButtonsにはHorizontalLayoutGroupを設定しています。

f:id:tomo_mana:20210813190018p:plain
JournalButtons - HorizontalLayout

ボタンを押した結果を表示するテキスト領域(JournalLog)はTextMeshProで作っています。

この2つのボタンとテキスト領域をEmptyオブジェクト(ImputWindow2)でひとくくりにして、VerticalLayoutGroupを設定しています。

f:id:tomo_mana:20210813185908p:plain
InputWindow - VerticalLayout


今回も手抜きして、InputWindow2に、全部のオブジェクトをまとめたコードを付けます。

コード

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class InputWindow2 : MonoBehaviour
{
    enum BUTTON_NAME {
        BUTTON_PURCHASED = 0,
        BUTTON_SALED
    };
    
    [SerializeField]
    BalanceSheet bs;
    [SerializeField]
    GameObject journalButtons;
    [SerializeField]
    TextMeshProUGUI log;
    
    // 試算表
    struct TrialBalance {
        public int cash;        // 現金
        public int purchase;    // 仕入(費用)
        public int sales;       // 売上(収益)
        public int pl;          // 損益
        public int q_remain;    // 在庫残
        public int q_total; // 在庫総購入量
    }
    TrialBalance tb = new TrialBalance();
    
    // 資本
    int firstCash = 10000;
    // 販売物の仕入単価(現在は固定)
    int unitPrice = 1000;
    // 販売物の売価(現在は固定)
    int unitSales = 1500;
    
    string[] msg = new string[]{
        "成功!",
        "現金不足!",
        "在庫不足!",
        "START!"
    };
    void UpdateLog(int error)
    {
        if( !ReferenceEquals(log, null) ){
            //現金:xxxx/仕入:xxxx/売上:xxxx/在庫:xx/xx(@100)
            log.text = msg[error] + "\n現金:" + tb.cash + "\n仕入:" + tb.purchase + "\n売上:" + tb.sales + "\n在庫:" + tb.q_remain + "/" + tb.q_total + "(@1000)";
        }
    }
    
    void InitializeJournal()
    {
        tb.cash = firstCash;
        tb.purchase = 0;
        tb.sales = 0;
        tb.pl = 0;
        tb.q_remain = 0;
        tb.q_total = 0;
        
        UpdateLog( 3 );
    }
    
    void ExecutePurchase()
    {
        int e = 0;
        if( tb.cash >= unitPrice ){
            tb.cash -= unitPrice;
            tb.purchase += unitPrice;
            tb.q_remain++;
            tb.q_total++;
            
            tb.pl -= unitPrice;
        } else {
            e = 1;
        }
        UpdateLog( e );
    }
    
    void ExecuteSale()
    {
        int e = 0;
        if( tb.q_remain > 0 ){
            tb.cash += unitSales;
            tb.sales += unitSales;
            tb.q_remain--;
            
            tb.pl -= unitSales;
        } else {
            e = 2;
        }
        UpdateLog( e );
    }
    
    void ButtonClicked(int buttonNo)
    {
        switch( buttonNo ){
        case (int)BUTTON_NAME.BUTTON_PURCHASED:
            Debug.Log("Button Clicked = " + buttonNo + "Purchased!"); // OK
            ExecutePurchase();
            break;
        case (int)BUTTON_NAME.BUTTON_SALED:
            Debug.Log("Button Clicked = " + buttonNo + "Saled!"); // OK
            ExecuteSale();
            break;
        }
    }
    
    // Start is called before the first frame update
    void Start()
    {
        int i = 0;
        Button button;
        if( !ReferenceEquals(journalButtons, null) ){
            foreach( Transform child in journalButtons.transform ){
                button = child.gameObject.GetComponent<Button>();
                if( !ReferenceEquals(button, null) ){
                    int ii = i;
                    button.onClick.AddListener(() => ButtonClicked(ii));
                    i++;
                }
            }
        }
        InitializeJournal();
    }
}

表示テスト

f:id:tomo_mana:20210813194009p:plain
表示テスト

少し雰囲気を出すため、背景を付けました。以下サイトの背景を使わせていただいています。
【フリー背景素材】背景制作会社 | アニメ背景美術会社CreativeFreaks


ゲームのURL(前回までと同じURLです)
tomo-mana.hatenablog.com

次回

貸借対処表を追加します。