using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class CardPicker : MonoBehaviour
{
#region Variables
[SerializeField] int test = 18;
// Editor completed fields
[SerializeField] List cardPool = new List();
// Random
private System.Random random = new System.Random();
int totalWeight;
#endregion
#region Functions
private void Awake()
{
ProcessTotalWeight();
}
private void ProcessTotalWeight()
{
totalWeight = 0;
foreach (Card card in cardPool)
{
totalWeight += card.weight;
}
}
private InventoryCard PickCard()
{
int roll = random.Next(totalWeight + 1);
int sum = 0;
foreach (var card in cardPool)
{
sum += card.weight;
if (sum >= roll)
{
return new InventoryCard(card);
}
}
return new InventoryCard(cardPool[0]);
}
///
/// Pick a specified amount of cards, adding them to the inventory
///
///
The amount of cards to pick
public List PickCards(int amount, List inventory)
{
List cards = new List();
for (int i = 0; i < amount; i++)
{
InventoryCard card = PickCard();
// Select the cards of the same type already present in the inventory
var sameCards =
from c in inventory.Concat(cards)
where c.type == card.type
select c;
// If there is at least a card present, it's level is ugpraded
int present = 0;
foreach (InventoryCard c in sameCards)
{
present++;
if (present >= card.maxAvailable)
{
c.level += 1;
break;
}
}
// Else the card is normally picked
if (present < card.maxAvailable)
{
cards.Add(card);
}
}
Debug.Log($"(CardPicker) Picked {cards.Count} cards");
// Return the picked cards
return cards;
}
#endregion
}
Я использую Unity 2022.3.20f1.
Может ли кто-нибудь мне с этим помочь?
У меня есть собственный скрипт юнита. Когда я добавляю его в игровой объект, свойства компонента не отображаются. Вот снимок экрана редактора Unity: [img]https://i.sstatic.net/TMZIROdJ.png[/img]
Я заметил, что в режиме отладки все отображается правильно: [img]https://i.sstatic.net/HrMNKZOy.png[/img]
А вот мой код: https://pastebin.com/vBpAxtkL [code]using System.Collections.Generic; using System.Linq; using UnityEngine;
public class CardPicker : MonoBehaviour { #region Variables [SerializeField] int test = 18; // Editor completed fields [SerializeField] List cardPool = new List();
// Random private System.Random random = new System.Random(); int totalWeight; #endregion
private InventoryCard PickCard() { int roll = random.Next(totalWeight + 1);
int sum = 0; foreach (var card in cardPool) { sum += card.weight; if (sum >= roll) { return new InventoryCard(card); } } return new InventoryCard(cardPool[0]); }
/// /// Pick a specified amount of cards, adding them to the inventory /// /// The amount of cards to pick public List PickCards(int amount, List inventory) { List cards = new List(); for (int i = 0; i < amount; i++) { InventoryCard card = PickCard();
// Select the cards of the same type already present in the inventory var sameCards = from c in inventory.Concat(cards) where c.type == card.type select c;
// If there is at least a card present, it's level is ugpraded int present = 0; foreach (InventoryCard c in sameCards) { present++;
// Else the card is normally picked if (present < card.maxAvailable) { cards.Add(card); } } Debug.Log($"(CardPicker) Picked {cards.Count} cards"); // Return the picked cards return cards; } #endregion } [/code] Я использую Unity 2022.3.20f1. Может ли кто-нибудь мне с этим помочь?
Я создал класс ScriptableObject MySO, который модифицируется в функции Awake(). Я также добавил EditorUtility.SetDirty(this); в качестве последней команды. Класс имеет поле List myList. Во время функции Awake я добавляю в этот список пару...
Я создал класс ScriptableObject MySO, который модифицируется в функции Awake(). Я также добавил EditorUtility.SetDirty(this); в качестве последней команды. Класс имеет поле List myList. Во время функции Awake я добавляю в этот список пару...
I'm currently facing an issue with my AICarScript in Unity, specifically with the path array being empty. I've implemented a waypoint-following system where the car should follow a path defined by child objects under a...