U
U
Uncle Bogdan2021-08-06 21:48:59
Unity
Uncle Bogdan, 2021-08-06 21:48:59

How to do without static methods?

There is an abstract class Component. It is inherited by all components of the computer.

The Component class has an abstract GetInfo method.
Each component implements it. And calls the ShowInfo method (turns on the canvas, displays information on the canvas).
The ShowInfo method is static.
This means that variables that store links to the canvas, text and other things will also be static. But then I cannot enter values ​​into these variables through the inspector. What to do?

Buy class
using UnityEngine;
using TMPro;

public class Buy : MonoBehaviour
{
    [Header("Processor info")]
    [SerializeField] GameObject _processorCanvas;

    [SerializeField] TMP_Text _processorPriceText;
    [SerializeField] TMP_Text _processorCoresAmount;
    [SerializeField] TMP_Text _processorThreads;
    [SerializeField] TMP_Text _processorSocket;
    [SerializeField] TMP_Text _processorName;

    private void ShowInformation(Processor processor)
    {
        _processorName.text = processor.Name;
        _processorCoresAmount.text = processor.CoresCount;
        _processorThreads.text = processor.Threads + "GHZ";
        _processorSocket.text = processor.SocketName;
        _processorPriceText.text = processor.Price;

        _currentProcessor = processor;

        _processorCanvas.SetActive(true);
    }


The basis
using System;
using System.Collections.Generic;
          
public class Program
{
public abstract class Component
{
  protected string _name;
  public Component(string name) 
  {
    _name = name;
  }
  
  public string Name {get { return _name; } }
  
  public abstract string GetInfo();
}

public class CPU : Component
{
  private string _freq;
  private string _cores;
  
  public CPU(string name, string freq, string cores) : base(name)
  {
    _freq = freq;
    _cores = cores;
  }
  
  public override string GetInfo() 
  { 
    Buy.ShowInfo(this) // Одна из перегрузок.
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Spartanec33, 2021-08-07
@Spartanec33

it's unclear. But if you really want through static fields and methods, then you can make an MB class for the inspector, from which the class with static things will take the value.

N
NoNameDeveloper, 2021-08-12
@NoNameDeveloper

MVC
(Model) Component and its descendants. Use ONLY for data (ex: name, price, sprite)
(View) You should have a prefab (ComponentView (as an example)) that accepts a Model (Component) and fill text and images with text and sprites from Model.
(Controller) Through it you have to fill Views with Models.
Simple example:

public class ComponentModel
{
    public string Name;
}

public class ComponentView
{
    private Text _nameTxt;

    public void Set(ComponentModel model)
    {
        _nameTxt.text = model.Name;
    }
}

public class ComponentController
{
    private List<ComponentModel> _components;
    public Transform Parent;
    public ComponentView ComponentPrefab;

    // Methods

    public void Awake()
    {
        Fill();
    }

    public void Fill()
    {
        for(int i = 0; i < _components.Count; i++)
        {
            ComponentView view = Instantiate(ComponentPrefab, parent);
            view.Set(_components[i]);
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question