Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I have an object that can come in various sizes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum Size
{
Small = 0,
Medium,
Large
}
public class BallSizes : MonoBehaviour
{
[SerializeField] private Size size;
}
After assigning the Size to various objects. I want to see how many objects are Small or Large etc.
Enum.GetName() and Enum.GetValue() seems to only return the names/values of the objects, instead of the total amount of objects that are of that type.
if you have a bunch of objects with different sizes, and want to count the number of each size, you could for example use LINQ
var counts = myObjects.GroupBy(obj => obj.Size).Select(group => (group.Key, group.Count());
Then for getting all your ball instances you could e.g. use FinsObjectsOfType
var myObjects = FindObjectsOfType<BallSizes>();
are better already store them in a list right when you instantiate them
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I need a buffer of memory which holds actual objects, not references, in memory.
I am using Unity Engine for my app. In order to submit a draw-call in Unity, one must provide a Mesh-object and Material-object in the function call. Here
is the part of my algorithm I am trying to optimize.
Mesh[] meshes;
Material[] materials;
Foreach (mesh-type)
DrawMeshInstancedIndirect(mesh[i],material[i]...etc);
I want to be able to iterate over a buffer of memory directly containing the object data, with no references, because I don't want to fill my cpu cache with useless data fetching each object in random parts of memory. You cannot substitute structs for these special Unity classes.
It doesn't matter if it is bad practice or a very messy solution. This is a performance-critical part of my app where structs cannot be substitute for classes.
Here is a reference to the draw-call function, for more context.
https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstancedIndirect.html
Try the fixed keyword.
class Foo
{
public int[] stuff = new int[999];
public unsafe void ExamplePinned()
{
fixed (int* pStuff = stuff)
{
//Alright, the data array is all set and ready to go. Think of 'pStuff' as a pointer
//to the first element in the array. You can use it to access the memory of the
//array and copy it to another buffer
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I basically want to go through the 2d List<> and deposit the sum of the items in a column to a 1d List. So far have a double for loop but I cant figure out the logic. Any help with how I can start to contruct it logically would be helpful. Thank you. This is the code I have so far:
So if you have this:
var grid = new List<List<int>> {
new() { 1,2 },
new() { 3,4 }
}
You can do:
grid.Pivot().Select(c => c.Sum());
And you'll get an enumerable that is { 4, 6 } (1+3 and 2+4)
You can get Pivot from fubo's answer here (or choose any one of the other suggested transforms)
If I misunderstood and you wanted {3,5} just skip the Pivot part
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Hi I'm a unity developer working on a bar manger game and I'm just wondering on the best way to implement a drinks system where it sets the value of the drink, number of servings in the barrel, name of the drink price of the whole barrel.
Here is some code I was working on before:
This is the interface method
public interface IDrinkSystem
{
string SetDrinkName(string nameToSet);
float SetDrinkValue(float drinkValue);
int AmmountOfServingsInBarrel(int Servings);
float PriceOfBarrel(float price);
}
This is the class method of doing it
public class DrinkSystem
{
public void NewDrink(string drinkName, float drinkValue, int barrelServings, float barrelPrice)
{
// Have getters and setters for all values in separate methods
}
}
What is the best way for making it easy to expand and at a push can I make an array of the NewDrink to store all the drinks i have or is there abetter way of doing this.
Use ScriptableObjects, as if they were files.
For every type of drink, have a ScriptableObject which you can fill in from Unity easily (even your artist can do this), and then at runtime, you load them into objects of a single DrinkEntity class which loads these values.
This way you don't end up with dozens or hundreds of "DrinkSystemBeer"/"DrinkSystemWhiskey"/etc classes, but you can still keep all your code clean and pattern-friendly.
IMPORTANT: NEVER operate with the ScriptableObjects directly. Simply load them into a DrinkEntity on its constructor. Treat ScriptableObject as if they were xml or json files from which you read your data.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
void Convert(string word)
{
var dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
if(dictionary[word] != null)
{
dictionary[word].ToObject<VocabularyModel>();
// do something;
}
else
{
// do something;
}
}
I am using (JObject)JsonConvert.DeserializeObject to convert json to var dictionary. Everything works fine. However I only have 1 json file and every time a use the function I have to:
var dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
Is there any way to call that line only one and the var dictionary is saved as global variable or something similar that help me call the above command only one time only.
And one other question: does the dictionary[word] works like a loop in an array? Does it go through the dictionary one by one and search for the key "[word]" ?
You can move the declaration of into a class-level scope.
class WhatEverYourClassNameIs
{
private var dictionary;
// Deserializing in constructor, but you can do it elsewhere
WhatEverYourClassNameIs()
{
dictionary = (JObject)JsonConvert.DeserializeObject(dictionaryJson.text);
}
void Convert(string word)
{
// This function and all other functions within your class will have access to dictionary
}
}
Regarding your second question, I don't have a concrete answer since the documentation doesn't say so. A normal .NET dictionary has O(1) indexing complexity, meaning that it doesn't take longer the more elements you have in the dictionary. If you want to be certain you'd have to look up the source code and see how indexing is accomplished in the JObject class
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
https://www.youtube.com/watch?v=K1xrlc32Tmw&list=PLJUoF2h8Z-brW94dTZ-ZIOhjFq90_lt5K&index=9
4:25 adds a new object in the lineCollection if product does not exist in lineCollection, but at 24:25 it shows duplicating orders? Did i misunderstood how it works?
https://github.com/jedjad/GitHubVS2013
Because the products in duplicate values are not same objects. They may have same names, quantity etc, but initializing a class with same values does not mean that it is the same object as the one initialized before. They are like 2 different apples with same color and size.
If you say that 2 products are same whenever the names are same, then implement IEquatable<Product> in Product class.
public bool Equals(Product other)
{
return Name == other.Name;
}