Первоначально делал через создание-удаление объекта, но наткнулся на проект в открытом доступе, который делает то что я хочу, но в 2Д. Проект.
В целом, как я понял идея в том что мы через ScriptableObject меняем спрайт на столе через слой.
p.s. Что собой представляет OverlaySprite, пустой объект? Почему он как префаб, но серый, хотя он активный.
Основной код:
CropAsset:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Crop", menuName = "Crop")]
public class CropAsset : ScriptableObject
{
public Sprite seedSprite;
public Sprite deadSprite;
public Sprite doneSprite;
public bool seedIsOnGround = false;
}
Crop:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Crop
{
public CropAsset asset;
public CropState state;
private float growthLevel;
private float waterLevel;
private bool isDead;
public bool Grow(float amount)
{
if (GetWaterState() == WaterState.Watered)
{
growthLevel += amount / 20f;
}
if (growthLevel >= 1f)
{
state = CropState.Done;
return true;
}
return false;
}
public WaterState Dry(float amount)
{
waterLevel -= amount / 8f;
return GetWaterState();
}
public WaterState GetWaterState ()
{
if (waterLevel > 0f)
{
return WaterState.Watered;
}
else if (waterLevel > -1f)
{
return WaterState.Dry;
}
else
{
state = CropState.Dead;
return WaterState.Dead;
}
}
public void Water ()
{
waterLevel = 1f;
}
public Crop (CropAsset a) {
asset = a;
state = CropState.Seed;
growthLevel = 0f;
waterLevel = 1f;
isDead = false;
}
public bool HasCrop()
{
if (asset == null)
return false;
else
return true;
}
public Sprite GetCropSprite()
{
if (asset == null)
return null;
switch (state)
{
case CropState.Seed:
return asset.seedSprite;
case CropState.Planted:
return asset.seedSprite;
case CropState.Dead:
return asset.deadSprite;
case CropState.Done:
return asset.doneSprite;
}
Debug.LogError("WHAT?!");
return asset.seedSprite;
}
public bool IsOnGround()
{
if (state == CropState.Planted && asset.seedIsOnGround)
return true;
else
return false;
}
public Sprite GetDoneSprite()
{
return asset.doneSprite;
}
public string GetName()
{
if (asset == null)
return null;
return asset.name;
}
}
public enum CropState
{
Seed,
Planted,
Dead,
Done
}
public enum WaterState
{
Watered,
Dry,
Dead
}
Tool:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Tool", menuName = "Tool")]
public class Tool : ScriptableObject
{
public Sprite sprite;
public ToolType toolType;
}
public enum ToolType
{
Plow,
Watercan
}
TableTile:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TableTile : MonoBehaviour
{
public SpriteRenderer overlay;
public Crop crop;
public Tool tool;
private void Start()
{
if (crop.HasCrop())
overlay.sprite = crop.GetCropSprite();
else if (tool != null)
overlay.sprite = tool.sprite;
}
public void Interact(Crop c, Tool t, PlayerInteraction player)
{
player.SetCrop(crop);
crop = c;
player.SetTool(tool);
tool = t;
if (tool != null)
overlay.sprite = tool.sprite;
else if (crop != null)
overlay.sprite = crop.GetCropSprite();
else
overlay.sprite = null;
}
}
Хочу сделать также, но для 3Д объектов. Мне порекомендовали поменять там где используется Sprite и SpriteRenderer на Mesh и MeshRenderer.
Мой код:
IngredientAsset:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Ingredient", menuName = "Ingredient")]
public class IngredientAsset : ScriptableObject {
public Mesh ingredientMesh;
}
Ingredient:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Ingredient : MonoBehaviour {
public IngredientAsset asset;
public Ingredient (IngredientAsset a) {
asset = a;
}
public bool HasIngrediend()
{
if (asset == null)
return false;
else
return true;
}
public Mesh GetIngrediendObject()
{
if (asset == null)
return null;
return asset.ingredientMesh;
}
}
EquipmentAsset:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Equipment", menuName = "Equipment")]
public class EquipmentAsset : ScriptableObject {
public Mesh equipmentMesh;
}
TableBox:
using System.Collections.Generic;
using UnityEngine;
public class TableBox : MonoBehaviour {
public MeshRenderer overlay;
public Ingredient ingredient;
public EquipmentAsset equipment;
private void Start()
{
if (ingredient.HasIngrediend())
overlay.mesh = ingredient.GetIngrediendObject();
else if (equipment != null)
overlay.mesh = equipment.mesh;
}
public void Interact(Ingredient c, EquipmentAsset t, PlayerInteraction player)
{
player.SetIngredient(ingredient);
ingredient = c;
player.SetEquipment(equipment);
equipment = t;
if (equipment != null)
overlay.sprite = equipment.mesh;
else if (ingredient != null)
overlay.mesh = ingredient.GetIngrediendObject();
else
overlay.mesh = null;
}
}
Но начиная с момента создания моих ScriptableObjects типа mesh, в моей версии Crop ругается на overlay.mesh.
Сам стол:
Overlay:
Прошу помочь разобраться как преобразовать данную функцию к 3Д объектам. Правильно ли то что мне подсказали? Правильно ли так делать? Можно также но с префабами?