#archived-code-advanced

1 messages · Page 103 of 1

gray parcel
#

Don’t know which piece it is tho and where the walls actually are

#

I am insantiating a room with walls

upbeat path
#

so add a script to each piece which does describe it's walls and exits

gray parcel
#

Or maybe I could add an empty gameobject to where the wall opening is and check which gameobjects overlap

#

If two empty gameobjects are im the same position it means they’re connected

upbeat path
#

problem is with that approach, you need to have placed the room first before you can check

#

you really do not want to be instantiating a room until you KNOW it fits

gray parcel
#

Oh so you think its better if i dont instantiate the room if its going to block an area

#

problem is, how do you know if that block is going to do that?

upbeat path
#

absolutely, unless that is part of your game strategy

gray parcel
upbeat path
#

Say each room has 4 bools, N,S,E,W. each one can be true (is a wall) or false (is an exit).
now you can compare a random block against it's neighbours

gray parcel
#

What do i look for?

#

Cause I need to check if that room blocks an area of the maze and it’s nit generated yet

upbeat path
#

well. this.N == roomNorth.S, this.S == roomSouth.N, this.W == roomWest.E and this.E == roomEast.W
obviously if there is no room at any one of those positions then the result of that test is always true

gray parcel
#

I’don’t get it, isnt N, S, W and W a bool? And if i compare those values ill get true if they both have a wall or if they both dont

upbeat path
#

exactly, so wall will always match to wall and exit will always match to exit

#

the way I do this is to use the following logic
bool?[] walls; // true is wall, false is exit, null is no room at position
then loop through the neighbouring rooms and fill that array with their S,W,N,E values // From North clockwise
then check all of my rooms that fit the array and write them to a List
randomly select from the List made
the room selected will always fit the space

gray parcel
#

I’m gonna try thanks

chilly anvil
#

Any suggestions on how to check if a NativeList is null? .IsCreated just throws a nullref exception if you call it on an uninitialized NativeList

public struct SimulationState : IEcsAutoReset<SimulationState> {
    public NativeList<EntityPosition> Positions;

    public void AutoReset(ref SimulationState c) {
        if (Positions.Length > 0 || Positions.IsCreated) // Either of these checks throw a nullref error
            c.Positions.Dispose();
        c.Positions = new(Allocator.Persistent);
    }
}
#

And if I try to call Dispose() immediately, it throws an exception saying that the list hasn't been allocated yet (which is true obviously)

sage radish
chilly anvil
#

I'll paste it, sec..

#
NullReferenceException: Object reference not set to an instance of an object
Unity.Collections.NativeList`1[T].get_IsCreated () (at ./Library/PackageCache/com.unity.collections@1.2.4/Unity.Collections/NativeList.cs:509)
***SimulationState.AutoReset (***.SimulationState& c) (at Assets/***/Scripts/Simulation/SimulationComponents.cs:33)
stuck plinth
#

IsCreated is itself just a null check really so i don't see how it could throw 🤔

chilly anvil
#

Yeah that's what's throwing me for a loop too

#

This is the offending line

public bool IsCreated => m_ListData != null;

and m_ListData is defined as

[NativeDisableUnsafePtrRestriction]
internal UnsafeList<T>* m_ListData;
stuck plinth
#

it's worth noting that in my version of the package at least, line 509 is inside Dispose, so are you sure it's not that IsCreated is returning true but the list is pointing to bad data?

chilly anvil
#

At this stage I haven't actually done anything to the list yet 🤔

#

AutoReset is called automatically from the ecs system when this component gets reused, and is used for cleanup

#

So ideally I'd just initialize it once and then Clear(), but there's no way of checking if it's already been allocated

#

Debug.Log(Positions.IsCreated); throws the same error

sage radish
#

Is there a reason why you're checking Positions, but disposing c.Positions?

chilly anvil
#

Ahhh

sage radish
#

I don't know how that explains the exception, though.

chilly anvil
#

You're right, I wasn't poking the new component. Let me see if I can untangle this..

#

Doink 🤡

#
if (!c.Positions.IsCreated)
    c.Positions = new(Allocator.Persistent);
c.Positions.Clear();
#

All good

sage radish
#

Nice. Still a weird exception, though.

chilly anvil
#

Yeah

#

Maybe something to do with the unsafe list

sage radish
#

The only thing that can be null is the m_ListData pointer, but the only way it's used is in a null check.

chilly anvil
#

Ah okay, I thought there might be more to it. That's weird then

#

Everything runs smoothly now though, thanks again

midnight violet
#

Not sure if this is advanced, but probably. If you take a package to overwrite its scripts (because its needed), do you need to copy the whole package or are you able to overwrite just individual script files somehow too? Guess not, but thought it might be worth a short asking here.

thin mesa
midnight violet
#

Dang it, thats what I was not hoping... thanks for clarification. Guess I have to live with the dirty way and not finished packages from unity side until they fix it themselves.

glad badger
#

I've got an asset from the asset store that is using hardcode paths to the root asset folder to dump a generated file that just confirms you've seen their little popup thingy after install. I always put my third party stuff in a "ThirdParty" folder but this asset keeps regenerating this useless file in my root so I'd like to fix it for them so its using relative paths and not messing up my beautiful folder hierachy lol

Whats the best way to detect the asset's current path relative to asset root folder to I can make it generate this folder within itself each time?

I was thinking adding a dummy file to the asset's subfolder with a unique file extension, then searching that file from the asset database to get its path and then editing their script to use that relative path. Is there a better way?

obsidian glade
glad badger
obsidian glade
#

if that's all the file it generates is doing then yeah, just set a "has seen the popup" bool in editorprefs

glad badger
#

yeah thats all it is, just confirms you've seen their greeting.

#

ok awesome thank you. Thats pretty clean

lilac lantern
#

You still know where the walls are since you placed them. What you need is some sort of data monobehaviour that keeps track of what direction is open to move to.

plucky terrace
#

Coroutine Best Practice: WaitUntil vs. While-loop yield return null

glad badger
#

well damn, that sucks. They have these hardcoded paths peppered all through their assets

#

they are using it for generated content as well

#

Maybe I could create a utility editor class singleton that hooks into [InitializeOnLoad] and keeps paths there. Just need to figure out the best way to have the editor class get its own asset path and use that to get the relative path to Assets root, and then update all their path code to build relative paths using that utility

upbeat path
glad badger
upbeat path
sick flame
#

Hello guys, this is not connected to Unity, but maybe you can give me some hint.
I need to parse and modify spir-v bytecode. Do you have any code samples for that?

brisk pasture
#

spir-v is a pretty well defined spec should be possible

#

but question is why not go back to the source level and work with what ever generated it in the first place

austere summit
#

Hello all! I'm using R3 + UniTask for my project and I need to implement feature:
in my game there is a turn state, when it changes some modules reacts (being subscribed to ReactiveProperty), for example there is a module that draws 2 cards at the beginning of each turn, also there might be a module that asks player to select a free card as a reward each 5 turns, both of those modules callbacks are async UniTask which means that I actually need to await them before change turn state to next turn.

Basically code which requests end of turn looks like this:

private readonly ReactiveProperty<ETurnState> _turnState = new(ETurnState.None);

public void Turn()
{
  _turnState.Value = ETurnState.End;
}

In perfect world I would do something like:

public async UniTask Turn()
{
  await _turnState.Set(ETurnState.End) // await all async callbacks reacting to end of turn
  
  // initiate next turn
}
jolly token
#

Something like List<Func<Task>> TurnEndTasks ?

austere summit
#

yes, this will work, but I'm looking for solution which will be consistent with R3 API if it is possible

jolly token
#

You want some bidirectional messaging, I don't think reactive extensions would/should cover it

austere summit
# jolly token You'd need your own. You can wrap that to make it look like observable interface...

Good point. Well, how can I organize it better then. What I want basically to achieve is to have game rules like Draw N cards each turn / Draft N cards each M turns as modules I can add and remove through my game config. That is why THEY should subscribe to some turn state (or just to Subject<Unit> NextTurn doesn't matter). List<Func<Task>> as custom awaitable ordered way to call async methods is good for me. Would be ok this way or should I figure out something more clean?

jolly token
#

So that your rules can be one of the subscribers

#

Though not sure you want it ordered or parallel 😄

austere summit
scenic pine
#

Hello, i initially created 2 tags that are still displayed in the object tag list, but not in the "added tags" list anymore.
I'm also unable to recreate the tags that are still displayed in the object tag list. I guess it's a bug, but is there any workaround that i could use?
Sorry if i'm not in the correct channel, i dont see any "support/bug" channel

sly grove
thin mesa
scenic pine
#

I'm truly sorry, i was 100% sure that i created those two tags before, i just checked in another project, and yes, those tags were created by default, my mistake.
Thanks for the help, and sorry for the bad channel use.

chilly anvil
#

Having some new issues when trying to serialize a NativeList to be sent over the network.

public struct WrappedPositions : INetworkSerializable {
    public NativeList<Vector2> Positions;

    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter {
        serializer.SerializeValue(ref Positions);
    }
    public override string ToString() {
        return $"[Positions has {Positions.Length} entries]";
    }
}

void MessageTest() {
    // Prepare test message
    var message = new WrappedPositions() { Positions = new(Allocator.Temp) };
    message.Positions.Add(Vector2.right);

    // Prints "[Positions has 1 entries]"
    Debug.Log($"{message}");

    // Write message
    using var writer = new FastBufferWriter(1024, Allocator.Temp);
    writer.WriteValueSafe(message);

    // Read message
    using var reader = new FastBufferReader(writer.ToArray(), Allocator.Temp);
    reader.ReadValueSafe(out WrappedPositions result);

    // Throws "ObjectDisposedException: The UNKNOWN_OBJECT_TYPE has been deallocated, it is not allowed to access it"
    Debug.Log($"{result}");
}
#

Same result if I try to give the reader a pre initialized NativeList

...
// Read message
using var reader = new FastBufferReader(writer.ToArray(), Allocator.Temp);
WrappedPositions result = new() { Positions = new(Allocator.Temp) };
reader.ReadValueSafe(out result);
...
#

Everything works nicely with other standard types, like a single Vector2 instead of a NativeList

chilly anvil
#

Arrays of C# primitive types, like int], and [Unity primitive types, such as Vector3, are serialized by built-in serialization code. Otherwise, any array of types that aren't handled by the built-in serialization code, such as string], needs to be handled through a container class or structure that implements the [INetworkSerializable interface.

stuck plinth
#

since it's not a *Safe method, i guess you also need to know the length ahead of time and call TryBeginRead yourself

chilly anvil
#

Interesting...

#

I'll poke around with this

#

Huh

#

So that got me a little closer! This seems to work, although I'm grabbing the NativeList inside the struct directly;

void MessageTest() {
    // Prepare test message
    var message = new WrappedPositions() { Positions = new(Allocator.Persistent) };
    message.Positions.Add(Vector2.right);

    // Prints "[Positions has 1 entries]"
    Debug.Log($"{message}");

    // Write message
    using var writer = new FastBufferWriter(1024, Allocator.Persistent);
    writer.WriteValueSafe(message);
    var writerBytes = writer.ToArray().Length;

    Debug.Log($"Writer has ({writerBytes} bytes)");

    // Read message
    using var reader = new FastBufferReader(writer.ToArray(), Allocator.Persistent);
    WrappedPositions result = new() { Positions = new(Allocator.Persistent) };
    reader.TryBeginRead(writerBytes);
    reader.ReadValueInPlace(ref result.Positions);

    // Prints "[Positions has 1 entries]"
    Debug.Log($"{result}");
}
#

Ahh okay, yeah it did work with ReadNetworkSerializableInPlace, as long as I know the length like you said!

#

So I guess I need to pack that with the network message itself

#

...but at that point, you might as well just use an array

dense frigate
#

Good morning,

I want to create a voxel game.

Currently I'm facing a problem and I can't find any way to do this:

I want to have several voxel maps for example:

map 1
map 2

the player can choose one or the other.

To create these maps of different sizes, I am completely blocked, with some research, it is often recommended to generate a voxel grid.

I have trouble seeing what happens next, that is to say that currently I know how to generate a terrain based on perlin, but how can I "keep" this generation to add decoration?

At this level, I was told about json files to place the elements, which seems super boring.

The way I see things I would like for example:

map1: 150 x 150 x 10 voxels

map 2: 100 x 100 x 5

So an efficient system for “generating” maps based on certain values.

Can anyone guide me?

untold moth
dense frigate
untold moth
#

Ok, then I don't understand the question

dense frigate
# untold moth Ok, then I don't understand the question

To put it simply, I'm having trouble knowing how to generate maps of different sizes and heights so that they can have their own decorations.

I use perlin for ground generation and add variations.

See it like call of duty maps, but voxel version

#

With 3D, I know how to do it, the problem is that the generation of voxel terrain is written in C# code, where in 3D I can simply add terrain in the scene and save it as prefabs

#

So currently I have for example a generated map of 100x100, to which I can add decoration, but how do I save the whole thing?

untold moth
#

what "whole thing"?

#

And what do you mean by "decorations"?

#

And what does it have to do with C++?

dense frigate
#

I mean C# sorry

#

Decorations like house, vehicles etc ..

untold moth
#

Well, you place them. Either procedurally or manually

dense frigate
#

To repeat what I said with call of duty, I would like to create a map on the theme "funfair" and another on the theme “forest of trees”

Except that currently I am generating a world, which I do not know how to preserve

untold moth
#

So what exactly are you asking? How to save your generated map? Or how to place "decorations" on it? Which is it?

dense frigate
#

the both , yes

#

I only have one piece of land, for the moment

untold moth
#

Well, I told you how to place "decorations".
As for saving it, you can serialize the data as a json or binary and save it to a file.

dense frigate
potent shoal
#

How do I load async from the resources folder using the new awaitable approach?
I rather not use a coroutine.

#
            ResourceRequest request = Resources.LoadAsync<TForm>("Prefabs/UI/Forms");
            await request;
            var prefab = request.asset as TForm;```
Will this do the trick?
#

😭 I have another 4 or so hours before I can test.

#

I am in the middle of a large refractoring process.

shrewd jolt
#

i have a tps character and im using a state machine, should i create a state for my character's aim state or should i check if im aiming in other states (if anyone can help i can send you the project, because something feels wrong)
the character's movement system changes while aiming

gray parcel
#

Would like to share my solution with other people, in case someone finds himself in the same position as I was. It might not be the best approach or the fastest but it works.
Hope it can be useful to other people in the future https://gdl.space/otokogapid.cs

rare silo
#

hello guys i have a question

im working on project for computer science final and one of the requirements is "searching or sorting (from iterative algorithm)". so i was WONDERING if GameObject.FindObjectOfType<>(); or one of unity's built in search functions is either a linear search or binary search or someething?? thnaks

sly grove
#

It's widely theorized to be a linear search.

rare silo
#

good enough for me

#

thanks

shut surge
#

Hey all, is there any way I'd be able to somehow hack in new C# features into Unity? (things like global using directives, new array initialisers, etc.)

thin mesa
#

sort of, you can upgrade roslyn in your project (which is not supported, but there are a couple of third party tools that help you do so) to get newer c# features that don't require newer .net versions.
you can also copy type definitions for a lot of things into your project from the c# source
none of that is supported or really recommended (for the most part)

fallen galleon
#

is there a good way to get the capsule position when it hit something from a capsule cast?

#

im trying to snap a capsule downward, i was doing trig to get the behavior i wanted but it was shakey (maybe as a result of inaccuracies?)
i need to get the blue point here, where the capsule above is the starting position, and the dotted capsule is where the capsulecast intersects with a plane

#

the raycasthit from the capsule cast returns the red point in the .point property

#

which makes sense, but what i really want is that blue point at the center of the capsule

jolly token
fallen galleon
#

what property? the red point is in there but i dont see any way to get the blue

#

oh i guess i could use the normal to get the center of the sphere

jolly token
fallen galleon
#

ohh... wacky

jolly token
#

Let me see if I can find

jolly token
fallen galleon
#

ah ty!

rancid oxide
#

I've built a script to connect to an API service via a websocket connection.
I need help on handling methods when I receive messages back.
Here's the script with only the parts that matter:

    {   
        switch (baseMessage.type)
        {
            case "audio_output":
                Debug.Log("Message type received: Assistant Audio");              
                HandleAssistantAudio(e.Data);
                break;
        }
    }

void HandleAssistantText(string jsonData)
    {
        var message = JsonConvert.DeserializeObject<AudioOutputMessage>(jsonData);
        string base64String = message.data;
        Debug.Log(base64String);

       SaveText(base64String); //This generates the error
    }

 void SaveText(string content)
    {
        string filePath = System.IO.Path.Combine(Application.persistentDataPath, "newText.txt");
        System.IO.File.WriteAllText(filePath, content);
        Debug.Log("Clipboard text written to: " + filePath);
    }```
This is the error I get: 
```Exception: UnityEngine.UnityException: FindObjectsOfType can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.```
I need a way to execute that code on the right thread but don't know how to do it.
surreal juniper
#

How are you sending this request? Normally you'd use UnityWebRequest.
To run code on the main thread search for "unity main thread dispatcher", there are several solutions.

Note that your SaveText method is prone to fail anyway. Any I/O operation MUST be enclosed with try/catch because it can fail for a variety of reasons on any system, such as permissions, some other app locking the file, or running out of disk space.

sly grove
#

That's what you want

#

NVM I'm slow

upbeat path
#

and I would suggest there is something in your AudioOutputMessage class that Unity does not like

rancid oxide
#

The documentation is unexistent and there are very few thread that discuss this topic with methods that are obsolete or don't work at all nowadays

#
    {
        var message = JsonConvert.DeserializeObject<AudioOutputMessage>(jsonData);
        string base64String = message.data;

        AudioClip audioClip = Base64ToAudioClip(base64String);
    }

    private AudioClip Base64ToAudioClip(string base64String)
    {
        byte[] audioBytes = System.Convert.FromBase64String(base64String);
        float[] audioSamples = new float[audioBytes.Length / 4];
        Buffer.BlockCopy(audioBytes, 0, audioSamples, 0, audioBytes.Length);
        AudioClip audioClip = AudioClip.Create("GeneratedAudioClip", audioSamples.Length, 1, 44100, false);
        audioClip.SetData(audioSamples, 0);
        return audioClip;
    }```

This are the current methods I have, I still cannot test it cause I have the thread topic to solve first
upbeat path
wind geyser
#

Hi, I'm new to Jobs/Burst, and everytime I spawn or destroy a unit, I re-setup the arrays, and I have 2 questions, isn't that expensive? And how do other developers do it?

untold moth
wind geyser
rancid oxide
#

this is the log:
"Exception: UnityEngine.UnityException: Construct_Internal can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function."

untold moth
rancid oxide
untold moth
upbeat path
rancid oxide
#

btw I confirm that this line is the issue:
AudioClip audioClip = AudioClip.Create("GeneratedAudioClip", audioSamples.Length, 1, 44100, false);

rancid oxide
#

This is the class, seems pretty simple to have errors

    {
        public string type;
        public string id;
        public string data;
    }```
#

it just gets the json data I receive from the message

untold moth
#

The issue is that you're using unity API on a background thread. That is not allowed. You need to sync to a main thread before calling it.

rancid oxide
#

"data" containts the audio in form of base64

wind geyser
rancid oxide
#

I have to somehow detach from that thread and do stuff on main I guess

upbeat path
untold moth
rancid oxide
#
IPVoid

Use this online base64 to MP3 tool to convert a base64-encoded string to MP3 format, so you can preview it in your browser and download it as MP3 file in your device. The simplest way to decode base64 as MP3 online.

upbeat path
rancid oxide
#

guess I'll first try to fix the thread stuff

#

maybe I can read about it on these sites

upbeat path
#

then you are reduced to trial and error

rancid oxide
#

since they seems to use the same system

upbeat path
#

all well and good. if they are doing a straight byte[] to base64string and back again. problem is AudioClip is expecting a float[]

rancid oxide
#

Ok so one step at time, I've fixed the thread issue, I've made a super rudimental script that checks every update if a string gets filled and when it does it tries to process the data into an audioclip. I know that's not ideal but I'll take care of it when the rest is solved.

using System;
using UnityEngine;

public class GenerateAudio : MonoBehaviour
{
    public HumeWebSocket humeWebSocket;
    public bool processingAudio;
    public AudioSource audioSource;

    void Update()
    {
        if(!string.IsNullOrEmpty(humeWebSocket.audioString))
        {
            if (!processingAudio)
            {
                processingAudio = true;

                Debug.Log("Generating Audio");

                AudioClip newClip = Base64ToAudioClip(humeWebSocket.audioString);

                audioSource.clip = newClip;
                audioSource.Play();
            }
        } 
    }

    AudioClip Base64ToAudioClip(string base64String)
    {
        byte[] audioBytes = Convert.FromBase64String(base64String);
        float[] audioSamples = new float[audioBytes.Length / 4];
        Buffer.BlockCopy(audioBytes, 0, audioSamples, 0, audioBytes.Length);
        AudioClip audioClip = AudioClip.Create("GeneratedAudioClip", audioSamples.Length, 1, 44100, false);
        audioClip.SetData(audioSamples, 0);
        Debug.Log(audioClip.name);
        return audioClip;
    }
}```
So far I get no error but an audioclip that is just noise.
Now its time to figuring out how to decode that string.
upbeat path
rancid oxide
#

Oh so UnityWebRequest works locally, intersting

#

I don't think that will be doable once played in webgl tho

upbeat path
#

sure it will

#

other option is not to send the audiodata via the websocket but to send the URL of the audiofile and then use WebRequest to get it

rancid oxide
stuck plinth
upbeat path
upbeat path
#

so you really have no idea of the data format of the audio file

rancid oxide
#

I've sent a message and directly asked them about that and all the necessary steps I'll need to do in order to decode the string

#

Hope that is the last piece of the puzzle

brave cairn
brave cairn
#

Allocator.Temp no need to be disposed, but can't be used in across job boundaries.

wind geyser
#

I have this job that executes int and TransformAccess, however I'm having issues scheduling it, can you please tell me what I'm doing wrong?

brave cairn
#

@wind geyser Looks like there is no count parameter

wind geyser
brave cairn
wind geyser
brave cairn
brave cairn
# wind geyser Like this?

No. Like this:

struct MovementJob: IJobParallelForTransform
{
  public int unitsCoresCount;
  public float deltaTime;
  ...
}

var job2 = new MovementJob()
{
  unitsCoresCount = unitsCoresList.Count,
  ...
}

job2.Schedule(...);
rancid oxide
#

@upbeat path I've made it!

#
using UnityEngine;

public class GenerateAudio : MonoBehaviour
{
    public HumeWebSocket humeWebSocket;
    public bool processingAudio;
    public AudioSource audioSource;

    void Update()
    {
        if (!string.IsNullOrEmpty(humeWebSocket.audioString))
        {
            if (!processingAudio)
            {
                processingAudio = true;

                ConvertIntoAudio(humeWebSocket.audioString);
            }
        }
    }

    void ConvertIntoAudio(string base64String)
    {
        // Decode the base64 string to a byte array
        byte[] audioData = Convert.FromBase64String(base64String);

        // Create an AudioClip from the byte array
        AudioClip audioClip = WavUtility.ToAudioClip(audioData, "audioClip");

        // Play the AudioClip
        audioSource.clip = audioClip;
        audioSource.Play();
    }
}

public static class WavUtility
{
    // Convert byte array to AudioClip
    public static AudioClip ToAudioClip(byte[] wavFile, string name)
    {
        int channels = BitConverter.ToInt16(wavFile, 22);
        int sampleRate = BitConverter.ToInt32(wavFile, 24);
        int subchunk2Size = BitConverter.ToInt32(wavFile, 40);

        float[] data = new float[subchunk2Size / 2];
        for (int i = 0; i < data.Length; i++)
        {
            data[i] = BitConverter.ToInt16(wavFile, 44 + i * 2) / 32768f;
        }

        AudioClip audioClip = AudioClip.Create(name, data.Length, channels, sampleRate, false);
        audioClip.SetData(data, 0);

        return audioClip;
    }
}```
#

Now I just have to pray that it works on webgl too 🙏

upbeat path
#

neat. You know this will only work for .wav files, right?

rancid oxide
#

every audio file I receive will be wav so its fine I guess

#

unless I'm missing something

wind geyser
upbeat path
#

someone throws a .mp3 at it and it goes bang. So you might want to throw some try {} catch {} stuff in there

rancid oxide
wind geyser
upbeat path
wind geyser
#

@brave cairn It's workiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiing!!!! EUREKAAAAAAAA

civic viper
#

I am making my own Web Socket for my VR unity game using 2 DLL libraries. the two DLLs are "websocket-sharp" and "Newtonsoft.Json". When I build the game to android everything works great until I try and open the APK. The APK opens and stays open but its stuck on a black screen. I'm assuming that I'm putting the DLLs in the wrong place. I'm also building with il2cpp.

upbeat path
civic viper
upbeat path
#

install it, it's a package in the package manager

civic viper
#

okay

upbeat path
#

don't decompile the dll's just make sure they are in a folder called Plugins

civic viper
#

they are in a folder called plugins

#

and the plugins folder is in my assets folder

#

i installed android logcat

#

whats the next step

upbeat path
#

build and run. But you might just want to read the docs first

civic viper
#

okay ill read the docs

#

i have built and run from unity and i have the android logcat window open

#

there is an error in logcat called "Not starting debugger since process cannot load the jdwp agent."

upbeat path
#

did you do a development build?

civic viper
#

i opened the game and i saw this in unity "2024/06/05 14:55:04.139 17896 17919 Error libprocessgroup set_timerslack_ns write failed: Operation not permitted
"

upbeat path
#

change to development build, it's awful difficult to debug without it, and try again

upbeat path
#

read what's in the log

civic viper
#

theres a lot is there somthing im looking for

upbeat path
#

I did say to read the docs, you can filter the logs, error might be a good thing to search for
you do realize this is code advanced, we expect a little initiative from posters here

civic viper
#
2024/06/05 15:07:25.324 18923 18950 Info IL2CPP 6/5/2024 3:07:25 PM|Fatal|WebSocket.Connect:0|System.Net.Sockets.SocketException (0x80004005): Connection timed out
2024/06/05 15:07:25.324 18923 18950 Info IL2CPP                             at WebSocketSharp.WebSocket.setClientStream () [0x00000] in <00000000000000000000000000000000>:0 
2024/06/05 15:07:25.324 18923 18950 Info IL2CPP                             at WebSocketSharp.WebSocket.doHandshake () [0x00000] in <00000000000000000000000000000000>:0 
2024/06/05 15:07:25.324 18923 18950 Info IL2CPP                             at WebSocketSharp.WebSocket.connect () [0x00000] in <00000000000000000000000000000000>:0 
2024/06/05 15:07:25.324 18923 18950 Info IL2CPP                             at WebSocketSharp.WebSocket.Connect () [0x00000] in <00000000000000000000000000000000>:0 
#

i know where this is in my code but im not sure how to fix it

#
            WebSocket = new WebSocket("ws://192.168.1.3:1534/Instance");
            WebSocket.Connect();
            WebSocket.OnMessage += OnMessage;
upbeat path
#

timeout is a parameter you can set for the websocket connection. You probably just need to set it higher, I generally use 2 seconds

#

that's a local IP address, are you sure that willl work?

civic viper
half swan
#

Not a delay, a length of time before it will be considered timing out

upbeat path
hushed fable
#

Though you'll hit the timeout regardless if you don't yet have any code ever sending anything 😄

civic viper
rancid oxide
#

So I tested the websocket stuff I've built few hours ago and it doesn't work on webgl (it gives an error) notlikethis

#

I've read somewhere that could be due to the WebSocketSharp library

#
You need a JS library to integrate the browser WebSocket API for Unity.```
No idea if its true tho
untold moth
rancid oxide
untold moth
#

Next time research your target platform limitations before moving on to the implementation

rancid oxide
#

I think that somewhat is still possible with workarounds

#

I just to test them 1 by 1 since feedback could be obsolete

untold moth
#

I mean yeah, if messing with the JS side, it's probably possible.

glad badger
rancid oxide
#

Have you tested it personally?

glad badger
#

nope but I will be using it (not for webgl myself)

#

they aren't using the shit http in unity

#

note though, that asset depends on 2 other assets by the same dev, the http and tls assets because his websockets impl is built on his http and tls

#

I think he has just a websockets bundle with the other dependencies for like $30

rancid oxide
#

Ok, thanks for the suggestion.
I will document myself on his stuff and then decide if to buy that

glad badger
#

yeah unity's http really is ass

#

it doesn't even support upgrade requests websockets itself

rancid oxide
#

You can remove the http part and still be right

glad badger
#

still need the handshake and upgrade from somewhere on http

rancid oxide
#

But I understand them, I guess there so few people that do this stuff that its not worth for them

glad badger
#

websockets in webgl or websockets in general? websockets are huge

rancid oxide
#

On webgl

glad badger
#

ah. I don't do webgl myself

#

some neat things out there though

rancid oxide
#

Webgl in general is such a unused platform for things that are more complicated than 2 squares moving

#

(hoping that webgpu will rise interest tho)

glad badger
rancid oxide
#

Last update was few weeks ago, nice

untold moth
idle saddle
#

Do any of you guys employ code obfuscation methods to prevent cheating in your games? If so, how do you do so. I'm not using it for anything just curious and doing some research

scenic forge
#

Code obfuscation is not security. Any protection method you use gives exactly the amount of protection you pay for it, in the case of a free obfuscation tool, that's zero.

plush hare
#

valve has near unlimited money to throw at this, and people are still easily getting around it

vestal condor
#

need some help here

i'm following this guide to do source generation: https://docs.unity3d.com/Manual/roslyn-analyzers.html

but then it says i need to make a new label RoslynAnalyzer for my .dll
however, i'm using the AddressablesAssets package and I can't figure out how to label my .dll in my case

jolly token
#

Dll will not be part of addressable asset

#

Source generator dll is not shipped

vestal condor
#

yea nevermind my editor bugged out and didnt allow me to set the asset label; thought it had to do with addressables changing how labels worked or something

#

but i still can't get the source generator to work lol

jolly token
#

Are you using correct version and target framework and all?

polar violet
vestal condor
vestal condor
#

let's say i do want to create these .cs files which are scriptable objects or monobehaviours

jolly token
#

Oh you need .meta for that

vestal condor
#

yeap

#

according to the bug report i linked, i don't think they intend to fix this

#

so let's say im content with just generating .cs files and putting them in the folder

#

what are some good approaches

regal olive
#

is there a place i can hire coders here? i need front/backend coders

jolly token
thorn flintBOT
vestal condor
#

for now i'm naively thinking of just writing a python script to do it, but a dumb python script wouldn't be aware of how i need to generate these files

in particular, i wish to go through a folder and pick out those .cs files whose classes are a subclass of a specific subclass (say some Factory)

and then for each of those, generate a corresponding .cs file defining a ScriptableObject

vestal condor
jolly token
vestal condor
#

okay, i see what you mean

regal olive
vestal condor
#

basically i have the partial classes already in the required folder

jolly token
vestal condor
#

well actually if i could already create those partial classes then i trivially achieve what i want, because the .cs files i wish to generate simply contain

using UnityEngine;

namespace XXX.XXX
{
    [CreateAssetMenu(menuName = "Status Effects/Bleed")]
    public class BleedSOFactory : StatusEffectSOFactory<BleedFactory> { }
}
#

and the reason i'm doing this is because i have a generic that already works; but scriptable objects can't be generic so it is just a trivial matter of setting the generic type

jolly token
#

Yeah it might be easier for just create some editor tool

vestal condor
#

i can see how this paradigm can work for more complex source generation needs

jolly token
#

with SerializedReference hack (Though I don’t like those)

vestal condor
#

but i guess my current use case is simple

jolly token
#

I feel like if this is small boilerplate then maybe best to just hardcode it

#

You could write Python script but it might take more effort than you save with it

vestal condor
#

i mean i do have like hundreds of such mono/SO classes to create

jolly token
#

Hmm I guess one thing you can do is copying the source file from source generator intermediate directory and dump it in Unity 😦

#

So it can have actual meta file.. lol

rancid oxide
#

So these are all the basic steps I need to do in my webgl project, they all works but I have some inconsistency problem on the first step:
The request to get the access token (UnityWebRequest.Post) is way slower than the editor, plus something it get errors (currently working on figuring out which)

#

On editor 100% of the requests succeds, on webgl version they do not. I don't understand why since the code is the same

stuck plinth
rancid oxide
azure meadow
#

can I brainwash a monobehaviour to believe that it's transform is actually coming from another gameobject?
(my problem: I have a behaviour from a library that changes transform, "its own transform" and I want to affect a different gameobject)

#

(I know I can try to setup some "following" between game objects, but at this point I might aswell duplicate the .cs file from the library 😛 )

sly grove
azure meadow
#

yep, I need to duplicate the .cs file from the package and use that 😅

grizzled lake
#

@azure meadow would this work for you?
public new Transform transform => someOtherObject.transform; (note the new keyword).

brisk pasture
#

seems pretty heavily XY problem, like just reference the correct thing on the start, or have this object expose that wanted reference on a component

bleak citrus
#

it's third party code

brisk pasture
#

modify the code, or make a transform follower component

#

would prolly just modify the code, and makes it use a serilzied field for the target transform. then maybe if its null on Awake get the current objects transform so it still works in its old usecase

jolly token
timber flame
#
  Container.BindInterfacesAndSelfTo<Employee>().FromComponentsInHierarchy().AsSingle();
  var employee = Container.Resolve<Employee>();
  Container.Bind<Character>().FromInstance(employee);

I want to bind child class (Employee), the parent (Character) and all interfaces. The above method is OK but I see a warning after every spawning
This installer has been assigned to the game object context.

Zenject Warning: It is bad practice to call Inject/Resolve/Instantiate before all the Installers have completed!  This is important to ensure that all bindings have properly been installed in case they are needed when injecting/instantiating/resolving.  Detected when operating on type 'Employee'.  If you don't care about this, you can disable this warning by setting flag

I have changed the zenject method BindInterfacesAndSelfTo and add base type as well but now I see runtime errors.

 public FromBinderNonGeneric BindInterfacesAndSelfTo(Type type) //baseType
        {
            var statement = StartBinding();
            var bindInfo = statement.SpawnBindInfo();

            bindInfo.ContractTypes.AllocFreeAddRange(type.Interfaces());
            bindInfo.ContractTypes.Add(type);
            // bindInfo.ContractTypes.Add(baseType);

            bindInfo.SetContextInfo("BindInterfacesAndSelfTo({0})".Fmt(type));

            // Almost always, you don't want to use the default AsTransient so make them type it
            bindInfo.RequireExplicitScope = true;
            return BindInternal(bindInfo, statement).To(type);
        }
stuck plinth
#

if you can't bind it directly to the Character type i think something like Container.Bind<Character>().FromResolveGetter<Employee>() would work?

dusky dawn
#

Hi, I'm using unity ILPostProcessor to generate some code on compile. On question, when I have 2 processors, it it possible to define which one executes first? it looks like they're being executed according to the order in the hierarchy

sage radish
dusky dawn
#

hmm, ok

#

thanks

glad badger
#

so with games using the mono backend, if you want to distribute patched DLLs you can just overwrite the old DLLs, assuming you have a modular DLL structure and not updating constants (which need a recompilation of anything using them) but what about when using IL2CPP? I'm not sure what the actual out put is like or if it would work the same way

timber flame
# stuck plinth if you can't bind it directly to the `Character` type i think something like `Co...

Thanks, I want to bind base parent Character, all its interfaces and Employee itself when the instance has Employee type. So, I can inject the employee instance as interfaces + Character + Employee in other components of the gameobject

public class ComponentA: MonoBehaviour{
   [Inject]
   private void Init(Employee employee){ //Employee instance (component)
   }
}
public class ComponentB: MonoBehaviour{
   [Inject]
   private void Init(Character character){//Employee instance (component)
   }
}
public class ComponentC: MonoBehaviour{
   [Inject]
   private void Init(IEntity entity){//Employee instance (component)
   }
}
dusky dawn
sage radish
spare gust
#

fixed?

steep herald
timber flame
#

Which one do you choose? There is ItemDefinition scriptable object with a prefab. Now, I want to instantiate different prefabs in different situations related to this item. For example item in the storage, item on the ground, item in the character's hands, etc.
It is worth mentioning it is a 2d game.
I can change GameObject prefab to something like below

// ItemDefinition SO
public PrefabData[] Prefabs; 
[Serializable]
public class PrefabData
{
   public string Key;
   public GameObject Prefab;
}

So, I can get the suitable prefab by key based on the situation.

Another way is to have only one prefab and then all item sprites in that prefab as child, then enable/disable them.
Which one do you prefer? and any better suggestion?

long ivy
#

what are the keys for? if they're those situations, that's a code smell in my opinion. Why have PrefabData at all? if you have different prefabs for different situations, why not have ItemDefinition just have those prefabs directly? ItemDefinition.StoragePrefab, .GroundPrefab, .HeldPrefab etc

hushed fable
wide mulch
#

So I want to implement a system similar to Minecraft datapacks, where you can define certain content for the game in JSON and have it work automatically. Does unity have something like this already, and if so, how can I use it?

#

I could lift the code from the game engine I was making before this project, but I want to try to do it natively if possible

regal lava
#

JsonUtility class or grab yourself the Newtonsoft json.net package

vestal condor
#

here's the setup for my question

  1. we know there is SerializeReference which can "serialize" interfaces
  2. i can get this to work correctly when i have class implementing said interface
  3. is it possible to get this to work correctly if i have a scriptable object or monobehaviour implementing this interface?
#

actually i just learnt that serializereference tends to break if you rename classes

dusty wigeon
# vestal condor actually i just learnt that serializereference tends to break if you rename clas...

Use MovedFrom attribute to rename serialized reference.

If you want to use a ScriptableObject instead of C# object, you can simply create a wrapper class. If the idea is to use UnityEngine.Object with interface, you should look around for tool that support that. As far as I know, there is no support in engine. The idea, in most case, is to also create a Wrapper with Generic. I also believe that you can use implicit conversion to make it seamless.

https://github.com/Unity-Technologies/UnityCsReference/blob/3f0dae724475e51dab2c924c4fa470cfd0269280/Runtime/Export/Scripting/APIUpdating/UpdatedFromAttribute.cs#L46

#

Alternatively, you can remove the needs by using component instead of inheritance. In other words, instead of having something like IDamageable, you have a component Damageable. This is usually my preferred approach given that interface require you to reimplement most of the work and multi-inheritance is not supported.

vestal condor
#

from my searching, MovedFrom only works once

#

you can't chain it like FormerlySerializedAs

dusty wigeon
#

You only need to make it work once don't you ?

vestal condor
dusty wigeon
#

If I am correct, ReImport change the data.

vestal condor
dusty wigeon
#

Not sure though

vestal condor
#

for quality of code purposes

vestal condor
#

scriptable object is just a way to contain data
really i'm writing a class that takes, say a List<IFoo>

#

for the cases where this class needs to be serialized, i will always use some ScriptableObject implementing IFoo

dusty wigeon
#

Why ?

vestal condor
#

but i also use this class in script, where it should really just be a list of interfaces

dusty wigeon
#

You should always strive to have the lowest abstraction level possible.

vestal condor
#

the lowest abstraction level is IFoo

dusty wigeon
#

Not really, if it is not needed.

vestal condor
#

i do need it lol

dusty wigeon
#

Not from what I understood.

vestal condor
#

i'm writing code for what is essentially an Action<X>

#

so it operates as C# object

#

ok you know what this is quite complicated to explain

dusty wigeon
#

Not sure why you would need a scriptable object to implement something similar to an Action<X>.

vestal condor
#

the Action<X> contains a field List<IFoo>

dusty wigeon
#

I mean, usually Action are unique to each instance, they are not shared.

vestal condor
#

basically it boils down to being able to build Action<X> both in code and by deserialization

dusty wigeon
#

By example, if you have a Door, which have a "OpenAction", you would usually serialized it straight on the door itself.

#

But, obviously, I know little to nothing to what you are actually trying to do.

vestal condor
#

by Action<X> i really just mean a function that takes in X and has no return type

#

and actually it's really an Action<X, List<IFoo>> which is a function which takes in X and List<IFoo> and has no return type
but via currying you can reduce it to a Function<List<IFoo>, Action<X>> which is a function that takes in List<IFoo> and returns Action<X>

dusty wigeon
#

What concretly are you trying to do ?

#

Weapon Skill ? Quest System ?

vestal condor
#

concretely i am writing an action that applies a status effect to a character

#

so X is Character and List<IFoo> is List<IStatusEffectFactory>

dusty wigeon
#

Why a factory ?

#

I mean, you need something like a factory, but why implement it directly

#

Really simple way of doing it is:

public class MyDebuffDefinition : DebuffDefinition
{
  public class MyDebuff : Debuff
  {
  
  }

  public Debuff Instantiate() {...}

}


vestal condor
#

i do have quite a complicated status effect system

dusty wigeon
#

You can pretty much implement everything with what I have shown you.

vestal condor
#

nevermind i think i'm not able to relay the intricacies of the system to you succinctly lol

dusty wigeon
#

You do you, but from experience, as little as I have, you never need to have a complicated level of abstraction.

#

It should come naturally, and by step.

vestal condor
#

i guess the upshot is that i can't just have the actual instances of these status effects floating around

#

there are fields in the status effects which can only be populated upon knowing which character the effect is on

dusty wigeon
#

The instantiate method is there for that.

vestal condor
dusty wigeon
#

Character attacks -> Ask Weapon if there is any weapon buff -> Instantiate Weapon Buff depending on the target hit.

vestal condor
#

wanting to abstract the application of this status effect as a serializable action is so that i can attach it onto various events, eg

  • upon taking damage
  • upon dealing damage
  • upon killing
  • etc etc
dusty wigeon
vestal condor
#

and i mean there are other actions i wish to attach onto these events

#

nvm i dont think i have successfully conveyed what im trying to do

dusty wigeon
#

Feel free to try again whenever you want.

vestal condor
#
public class GiveCharacterStatusEffect : IAction<Character>
    {
        [SerializeField] // I cannot serialize arbitrary interfaces
        private List<IStatusEffectFactory> statusEffects;

        public void Invoke(Character character)
        {
            List<StatusEffect> toApply = statusEffects.ConvertAll(x => x.GetStatusEffect());
            character.StatusEffectHandler.AddEffect(toApply);
        }
    }
#

the fix is really just to change IStatusEffectFactory to StatusEffectSOFactory, where StatusEffectSOFactory : ScriptableObject, IStatusEffectFactory

#

but this stops me from using this class purely in script

dusty wigeon
#

You either use Editor Tooling as I said earlier, or you use StatusEffectSOFactory like you did.

vestal condor
#

i mean what i can really do is

public class GiveCharacterStatusEffect<S> : IAction<Character> where S : IStatusEffectFactory
    {
        [SerializeField]
        private List<S> statusEffects;

        public void Invoke(Character character)
        {
            List<StatusEffect> toApply = statusEffects.ConvertAll(x => x.GetStatusEffect());
            character.StatusEffectHandler.AddEffect(toApply);
        }
    }

public class GiveCharacterStatusEffect : GiveCharacterStatusEffect<StatusEffectSOFactory> {}
#

all this is just to get a serializable status effect factory, but as i said if i did SerializeReference then the serialized data is not resistant to refactors

dusty wigeon
#

Can you not restrict on a ScriptableObject and an Interace ?

dusty wigeon
#

I mean, you want to have the ability to use other type of object than ScriptableObject or not ?

#

Because, you either serialize UnityEngine.Object or POCO.

vestal condor
#

i mean yea as it stands this class is still a C# class

then i would proceed to have

class MonoAction<T, X> : MonoBehaviour, where X : IAction<T>
{
  public X action;
}

class GiveCharacterStatusEffectMono : MonoAction<Character, GiveCharacterStatusEffect> { }
vestal condor
dusty wigeon
#

You could, by example, have your UnityObject be serialize through a wrapper. This would enable you to support ScriptableObject and non ScriptableObject reference.

public class GiveCharacterStatusEffect<S> : IAction<Character> where S : IStatusEffectFactory
{
    [SerializeReference]
    private List<S> statusEffects;
}

public class StatusEffectFactoryUnityReference : IStatusEffectFactory 
{
  
}
vestal condor
#

yea i've basically been adopting this pattern where my C# object is serializable and my scriptable objects just wrap the C# object

#

so my problem now is that my C# object isnt always serializable

in this case this particular action isnt serializable unless i choose a particular representation for the statuseffectfactory, ie the scriptable object form

dusty wigeon
#

Anyway, my approach would be something like that.

public class BuffDefinition {}
public class BurnDebuffDefinition
{
  
}

public class Equipment 
{
  [SerializeReference]
  public List<Effect> effects;
}

public class Effect {}
public class BuffEffect 
{
  [SerializeField] 
  private BuffDefinition buffDefinition;
}

public class Character {
  public class Attack() {
    ...
    ApplyAllAttackBuff(target);
  }

  public class ApplyAllAttackBuff(Target target)
  {
    var effects = Equipment.Get<BuffEffect >(); 
    foreach(BuffEffect effect in effects)
      effect.Apply(target);
  }
}

vestal condor
#

yea this is what i did earlier in the project before i had to consider more complications

#

hence all the funny abstractions that eventually came about

dusty wigeon
#

It seem you are still trying to understand those complication.

#

Because I would ask what complication, but I am pretty sure you would have a hard time to say.

vestal condor
#

yea but basically each damage instance goes through a ridiculous amount of mutation before application

dusty wigeon
#

Obviously.

#

Pretty much any RPG face that.

vestal condor
#

yea so for instance the equipment might not know what effects to apply

dusty wigeon
#

The function Get can be aware of the context.

#

If not, the function Apply could be joint with a function CanApply

vestal condor
#

i mean by not abstracting it into an action system it really becomes ridiculous to implement the other things

dusty wigeon
#

Effect is more than enough abstraction to be able to apply it with other system

vestal condor
#

like there can be arbitrarily many things that can occur

regal lava
#

I ran into a funny design problem where I'd cache my buff effects (calculating damage/time) as soon as I equip the ability/equipment but eventually ran into an issue where I would recursively cache similar data if say I had some effect that proliferated between objects

dusty wigeon
#

Those things can be fix whenever they need to be fix.

vestal condor
#

i just brought up application of status effect as a particular case where the pattern doesnt work

#

solving this particular problem doesnt actually solve it for my general case

dusty wigeon
#

Ok, let's say we want a Quest to apply a Debuff.

vestal condor
#

no its not necessarily just applying debuffs

#

it could be literally any function that takes in Character

dusty wigeon
#

Ok, let's say we want the attack to be duplicate whenever the Character attacks

#

Would that be enough for you ?

vestal condor
#

as i said im really just looking for a clean serialization solution lol

dusty wigeon
#

I gave you earlier all the possible solution for serialization. There is no other as far as I know.

vestal condor
#

its not just Action that needs to be serialized correctly

dusty wigeon
#

Personally, I do not need any editor tooling as I use the pattern I have shown you and they are more than enough in all the case I faced. Which, some were complicated.

regal lava
#

and just check for them in code when doing damage, ect

dusty wigeon
regal lava
#

Maybe? I'd just gone by my own design choice with it after trying a bunch of ideas.

dusty wigeon
#

I mean, the enum is basically the Type while the delegates is the interface method.

regal lava
#

Having enum -> dictionary delegate lookup to trigger a behavior where I've kinda landed on

vestal condor
#

yea i just dont like how i need to compromise on code quality to make things work with unity

#

enums are also not resistant to refactors

dusty wigeon
#

Enums are usually an Obsessives Primitive violation.

regal lava
#

and mackysoft's serialized references

dusty wigeon
timber flame
vestal condor
timber flame
#

What do you think? URP rendering pipeline and srp compatible shader
Batch count is huge but in the frame debugger, you can see it is low 29

untold moth
regal lava
#

doesnt help with gpu instancing either

dusty wigeon
deep jasper
#

Is unity suitable for world streaming(chunk loading/unloading)

#

or i should consider switching to unreal/smth else

#

If it is, where can I start from(some resources/videos) to learn how to do it

dusty wigeon
deep jasper
#

Or maybe it's possible to load the entire terrain grid and disable most of terrains

deep jasper
#

Somebody also mentioned having each chunk as a separate scene, but idk if loading/unloading the scene is not going to cause lag spikes

pallid herald
#

heya, im developing a snow deformation shader, and i'd like to add ray to mesh collision using a compute shader, but i heard those can be quite expensive, so is there any way to make it more efficient than just checking a bunch of ray collisions for each triangle of the mesh?

#

obviously a low poly mesh and bounding box are my first instinct, but what can i do if i have a deforming mesh?

#

also, all the rays are going straight up if that helps

dusty wigeon
deep jasper
#

I just need to spawn objects

#

Lots of em

#

When player is around

dusty wigeon
hardy jacinth
#

Help, OnTransformChildrenChanged() isn't getting called in my code.
Does anyone have some idea wth is going on?

#

The code is basically:

void OnTransformChildrenChanged() {
    refreshChildren();
    OnStructureChanged();
}
#

and I am trying to use this even in editor component

#

more precisely when I drag a game object in hierarchy from one parent to another

#

this smells me like something Unity can't really do

#

or can it?

long ivy
#

if you're testing it in edit mode, I assume you have decorated the script with ExecuteInEditMode or ExecuteAlways?

hardy jacinth
#

I decorated the method...

#

...so when I added it to class it now works

#

👍

#

thanks

#

although it seems like this event is triggered before children change, and not after

#

is there some way to get a list of children that changed? More precisely the new children

long ivy
#

you might want to double-check since that's not the behavior I see

hardy jacinth
#

which Unity version are you on?

long ivy
#

2022.3.29f1

hardy jacinth
#

hmm, I am 2022.3..32f1 so I doubt it's that

#

Just to be clear: we are talking about MonoBehavior, not UIToolkit or anything

long ivy
#

yes

hardy jacinth
#

okay, thanks, that's the error on my part

#

it works as intended

#

I have filtered by the component type, but I add component only after reparenting

#

that's why it's not included

deep jasper
umbral wasp
#

I have a Dynamic Music System where I can do stuff onBeat.
I have delaget which I can subscribe to, to do stuff on other script synced to my music.
The problem is, sometimes the event is happening twice (maybe in the same frame dont know) so I can for example switch around a bool or so.
Im starting a endless Couroutine when I initialize the Music System like so:

    {
        while (true)
        {
            yield return new WaitForSecondsRealtime((float)timeToNextBeat);
            if(onBeat != null)
                onBeat();
        }
    }
#

on a debug it often looks like this, onBeat is even worse then onBar.
I dont know how I can fix it or what I did wrong in the first place.
Keep in mind timeToNextBeat is double because its synced to the AudioSettings.dspTime to make it more accurate.

#

So basically what I want is a Event System that is framerate independend but because I want to do stuff that is not I did this. Any ideas?

untold moth
#

If you want to be completely independent from the main thread loop, you'll need to run your logic on a separate thread.

umbral wasp
#

will not work, because it has to run on WebGL

#

so I need a solution to filter out second calls somehow.

untold moth
umbral wasp
#

I know that the loop isnt following precisly but the second call is weird

untold moth
umbral wasp
#

but I dont need the visuals to follow the beat exactly

untold moth
#

Are you sure that the 2 calls are executed on the same frame?

umbral wasp
#

currently like this, dont be confused with the wrong naming and stuff, im in the "WTF is going on phase" 😄

umbral wasp
untold moth
#

Print the frame number

umbral wasp
#

how? 😄

untold moth
#

And if they're not, then what is the problem?

umbral wasp
#

that two events are called way to close to each other

#

which shouldnt be the case

umbral wasp
#

ah thx

#

not at all on the same frame 😄

untold moth
#

I wonder if the wait for seconds param is 0, would it execute on the same frame or skip one?🤔

untold moth
#

Or too close

umbral wasp
#

haha that my stupid animation transition sometimes just skips back

#

I will record a short video

untold moth
upbeat path
umbral wasp
#

I just use a animator bool parameter to switch between animations without a transition, goal is to have a transition in there.

#

The scale down thingys are also subscribed to the same event, but the script in there doesnt seem to bother lol

#

maybe this helps.
its Framecount and the timeToNextBar

dusty wigeon
deep jasper
#

if each chunk terrain is unique

#

Idk if object pooling can help me by instantiating 10000k disabled terrains

#

and enabling/disabling them at runtime

glad badger
#

definitely better than instantiating them on the flying and garbage collecting them, over and over.

#

do you mean 10000k terrain chunks?

#

if so, is that really all going to be visible to a player at once?

glad badger
#

then you definitely want to implement a form of pooling. Since this is for terrain, pooling might not even be the way. Usually when people do mesh-based terrain, they generate the chunks dynamically around the player as the player moves through the world (on the client) so, rather than pooling, the chunks are always there and just the height data and materials are updated.

#

so you'd have your LOD0 chunks nearest the player, then the LOD1 surrounding those, etc

#

and if its multiplayer, the server will operate completely different; either all the terrain will be loaded at LOD0 at all times or you would load up all relevant zones chunks that have players within them and unload ones that don't

sonic hinge
#

Hi everyone,

Situation - I'm designing a construction tool which generates railway tracks procedurally (the same way Cities Skylines does for roads).
Issue - My concern is about limiting the cost of collision checks for intersections when preview is rendered, to prevent tracks from crossing each other when built. My curve is sampled over a quadratic Bézier, meshes are generated to connect each sample and are merged together using CombineInstances.
Thoughts - Where this issue would have been OK for only straight tracks, I'm wondering how to best design my architecture to handle curved tracks intersection efficiently. As they are roughly composed of ~40 consecutive straight segments, I'm not feeling like as many box colliders would do the job checking against all the already built segments (which could easily rise up to more than 10,000 box colliders).

My best bet is currently on Physics.OverlapBox() but I'd like to hear your opinion on the matter. Thank you.

sage radish
sly grove
#

Indeed^

And if you do that - I would recommend taking a similar approach to the physics system. Give each section of road an AABB (axis aligned bounding box) and use a QuadTree/OctTree or other spatial acceleration structure to do a very performant initial "broad phase" intersection check.

That will cull the potential candidates for a collision down tremendously. At that point you can use something like the math described here: https://stackoverflow.com/questions/4039229/checking-if-two-cubic-bézier-curves-intersect in order to check for the actual collision of two bezier curves, or maybe even just compare all the straight line sections in a relatively naive way.

sonic hinge
#

@sage radish They don't need any collider except for this purpose. I think I've made up my mind and I'll use the track MeshRenderer bounds AABB to discard most tracks, and loop over each track segment within it checking for line intersection. That should be efficient enough 🤞
@sly grove I thought of implementing my own octree/BVH/whatever spatial partitioning but iirc Unity physics engine already implements theirs so I'd have preferred not to reinvent the wheel and take advantage of it. I guess I'll just benchmark with my solution above, I just ignore how well Unity physics engine partitions space dynamically as expanding bounds of such structure can be very quickly problematic.

deep jasper
#

i made them relatively small(128x128)

deep jasper
uneven root
#

Hey all, I'm trying to do something a bit unusual. I would like to compile a C# code from a string at editor time, and to do so I'm using the CSharpCodeProvider module. It's working nicely for basic script but I would like to give access to the UnityEngine namespace to my script, especially for the Vector3 type. I've something like this :

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateInMemory = true;
parameters.GenerateExecutable = false;

//This alone produce the error : The type `System.IFormattable' is defined in an assembly that is not referenced. Consider adding a reference to assembly `netstandard, Version=2.1.0.0,
parameters.ReferencedAssemblies.Add(typeof(UnityEngine.Vector3).Assembly.Location);

// So I tried to add this but then get this error for almost all possible type : The predefined type `System.Object' is defined multiple times. Using definition from `mscorlib.dll'
parameters.ReferencedAssemblies.Add(typeof(System.IFormattable).Assembly.Location);

string code = scriptPrefix + script + scriptPostfix;
CompilerResults results = provider.CompileAssemblyFromSource(parameters, code);

As noted in the code sample, I don't understand the proper Assembly reference to pass to get it working. Does someone have an idea ?

hardy jacinth
#

what you are trying to do seems to me like a code smell

uneven root
#

@hardy jacinth So we have an Editor Extension to generate animation curve with a graph editor, keyframe and more... and we would like to add a "simple" script editor that would allow to manipulate those curve to perform complex operation on them. It all happen on the editor side, so obviously we could simply create those expression in a C# file and have a workflow working with that, but that tools is intended for our animator so the goal is to have something a bit more streamlined and tightly integrated to the graph editor. I don't know If you know After Effect but the end goal is something similar to their expression engine based on Javascript.
For ease of development and generating the scripting API I wanted to make the expression engine evaluate C# code, that why I looked at the CSharpCodeProvider.
After some research I also found this unity asset : https://assetstore.unity.com/packages/tools/integration/roslyn-c-runtime-compiler-142753#description
Which is based on Roslyn that seems more robust and recent that CSharpCodeProvider. I Think i'll give it a shot to see if it's easier to implement.

hardy jacinth
#

Hmm, okay. I don't know the expression engine in after effects so I am not sure what kind of complicated expressions you have in mind here.
My thoughts for your case:

  • if the tool is meant to be used in the ditor, then you shouldn't need roslyn-c-runtime-compiler. This tool is, as far as I can tell, meant to be used in runtime when the game is built
  • using a C# language as a scripting language for curve editor seems like a massive overkill. You should instead consider the following:
    • would the user of that tool be able to use it comfortably? An animator may not be a programmer, remember
    • how extensive would the API for the scripts would have to be? Is the use case even realistic, or is it a set of 4/5 fixed functions that can be hardcoded and added on-demand by programmer when animator requests them?
    • maybe you don't need a full blown scripting language, only a simple expression evaluation model defined with simple expressions?
  • Are you sure you are trying to solve the right problem? Would implementing the solution otuside of unity be a better approach? Maybe you should focus on proposing to change the workflow instead? Something like rigging and animating in blender (blender has expression driven animations as well) and only exporting the animations and handling blending and event-driven animations in unity? I am simply
  • what are "complex" operations? Try listing them and look at the problem from a broader perspective
  • if you want to generate C# scripts you could simply write to .cs scripts in the unity and let IT handle the recompilation. No need to compile by hand if Unity can do that already.
  • possibly instead of writing scripts you could write a set of modular components that are fixed and composable, just like nodes in the ShaderGraph, and write an asset importer that builds a runtime model of an expression?
  • Roslyn may be an overkill
  • C# compiled expressions can be an alternative
  • compiling by hand in unity is very risky
sage radish
hardy jacinth
#

I've been in a similar situation once. The request was to write a tool that would help with rendering textures of models imported and positioned in unity. The artist's workflow looked somewhat like this:

  1. They created a model, textures etc. in Blender
  2. They imported everything to unity
  3. They applied a skeleton and one of the fixed poses to the models (with some small adjustments if needed) and rendered it on a transparent background to create fixed textures

The request was to improve the process, because they have been using the tool written previously in unity and had to manually click on "render" in the editor tool for each model and the whole process was tedious. A "batch" tool was proposed, while the ideal workflow would be to... simply skip importing into unity all together and handle that in the modelling software...

#

Maybe I am wrong because I don't see a broader picture for your case, so maybe some concrete examples of what those "advanced operations" on generated curves look like could help me to understand? Perhaps some screenshots of what your curve tool looks like?

uneven root
#

@hardy jacinth Wow, that's a detailed answer. Thanks a lot for that. Yeah I guess it's completely overkill to use C# for a scripting language no doubt. Among your proposition there are 2 that I'll definitively investigate more :

  • Writing to a .cs file and let unity handle the recompilation
  • Using modular component to make a node based graph, that seems interesting.

So those operation are mostly math / physic. For example I could have 2 curve drawn by hand representing the Amplitude and the Frequency of a Sinus and therefore I need an expression to compute the value of the Sinus at a time t, given the amplitude and frequency at that time. Or it could be a hand drawn curve of a Vector3 and the need to compute the Rotation around that vector based on the angular velocity defined by another hand drawn curve. But it could go all the way to I don't know, generating the wave function of a ripple on a water surface. This is not for gaming development, it's more related to physics research and R&D, it's about generating signal to control Realtime electronics and device with Realtime sensor monitoring.
But you gave me good lead on how to tackle this problem, I'll think about how to approach it a bit more.

@sage radish Thanks, I'll take a look at that.

short junco
#

Hey all ,

When I try to serialize and deserialize a normal scriptable object with newtonsoft.json, it throws a warning saying that it cannot be created with scriptable object new() creation.

I made a custom so converter to solve this, but while it doesn't give an error in normal default converter, I get an error like this in custom converter:

Self referencing loop detected with type 'GP.Serialization.DataTypes.GPGraphData'. Path ''' .

In order not to get this type of error, for example vector2 and vector2.normalized were causing the same problem, but I solved it with custom implementation, what is the reason why this is happening now?

    public class GPSOConverter<T> : JsonConverter where T : ScriptableObject
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(T).IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            T tempObject = ScriptableObject.CreateInstance<T>();
            serializer.Populate(reader, tempObject);
            return tempObject;
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }
    }
hoary cove
#

Hello devs, I'm looking for a way to generate NavMesh at runtime through multiple threads or any way that could reduve performances. We're currentily making a game where we procedurally generate flying islands with a chunck system but we have issue with generating the navmesh for ai to stand on because we regenerate navmesh each time a player leaves a chunck to another one.

We are using Unity 2022.3.16 and Unity new nav mesh surface system to generate navmesh at runtime.

If there's no way to use multi thread operation, will there be a way to aproximate a first navmesh with less precision and then, when said, update this navmesh ?

dusty wigeon
grave raft
jovial oriole
#

try this ```// Update method
void Update()
{
for (int i = 1; i <= nx; i++) // Corrected loop bounds
{
// Flux calculations and updates
}

// Update ghost cells
density_new[0] = density[1]; // Reflective boundary conditions
density_new[nx + 1] = density[nx];
momentum_new[0] = momentum[1];
momentum_new[nx + 1] = momentum[nx];
energy_new[0] = energy[1];
energy_new[nx + 1] = energy[nx];

// Copy values back to original arrays
Array.Copy(density_new, density, nx + 2); // Corrected array length
Array.Copy(momentum_new, momentum, nx + 2);
Array.Copy(energy_new, energy, nx + 2);

}

delicate mural
#

Anyone here have experience with making physical items?
I've created a very rudamentary system by setting velocity to go straight to the hand. It works, but it's kinda shit

         rb.linearVelocity = (targetPosition - grabPoint.position) / Time.deltaTime;
         (targetRotation * Quaternion.Inverse(grabPoint.rotation)).ToAngleAxis(out float angle, out Vector3 axis);
         rb.angularVelocity = angle * Mathf.Deg2Rad * axis.normalized / Time.deltaTime;```
What's the next step to making this better?
jovial oriole
#

I had a friend over here earlier and he made the suggestions

jovial oriole
# half swan Is this from chatgpt?

he was looking over my shoulder as i was asking about my own questions with my game, he knows a few coding languages but nothing about unity

#

if it doesnt work sorry, if it does i will let him know lol

half swan
jovial oriole
#

i looked up chatgbt... seems like its for story telling

wide urchin
#

my menu doesn't want to show up someone helps me

jovial oriole
#

code from AI would only make me more c-onffused

tall ferry
wide urchin
tall ferry
wide urchin
#

FDDS

wide urchin
jovial oriole
steel snow
#

with unity jobs is it bad to use them every frame ? would that be performance heavy since i presume jobs have to create threads and dispose all the time ?

#

are they best just for big processing jobs every so often

wide urchin
#

my menu doesn't want to show up someone helps me

steel snow
#

i have to apply a lot of TRS matrices to my data so want to do it in parallel

wide urchin
#

my menu doesn't want to show up someone helps me

tall ferry
wide urchin
austere jewel
#

!mute 1130192242787758142 30m do not spam

thorn flintBOT
#

dynoSuccess kzodeasg was muted.

austere jewel
#

This is absolutely not an advanced (nor code?) issue regardless

scenic forge
steel snow
#

so they are probably pooling and reusing the jobs then ?

scenic forge
#

In my game I have something that does job.Schedule(...).Complete() every frame and it works fine.

steel snow
scenic forge
steel snow
#

ah i see

#

are you also creating and disposing native arrays every frame or using a persistant and reusing it

scenic forge
#

Reuse wherever possible for sure.

steel snow
#

see the docs says persistant native arrays add perf cost

#

so i assumed temp ones were better

scenic forge
#

The entire job struct is only created once at the start and just keep rerunning it.

scenic forge
#

But if you are only allocating once then it's irrelevant, "slower allocation at start up, and never need to allocate again per frame" is still way better than "faster allocation but have to do it every frame."

steel snow
#

well the docs says Don't use Persistent where performance is essential.

#

so i dunno how to interpret that 😄

scenic forge
#

When in doubt, profile it.

steel snow
#

true

scenic forge
#

At least the last time I profiled "persistent allocator + reuse" vs "temp allocator but reallocating every time" the former vastly outperforms.

plush hare
#

you said you're working with matrices, so you might want to look into some sort of AVX/SIMD type stuff if you really want to go full turbo mode

steel snow
#

pretty sure burst uses SIMD with its matrices from the unity math api anyway

#

so i shouldn't need to worry about that

plush hare
#

no clue. typically SIMD operates on massive batches of data rather than one time calls

#

I've never done SIMD on matrices so I could be totally wrong here

#

the performance you can get from proper SIMD totally blows multithreading out of the water though. it's just a real pain in the ass to do

scenic forge
#

I've never looked into it, but supposedly Burst compiler which uses LLVM under the hood, does auto vectorization. So code like for (var i = 0; i < length; i++) { /* do stuff with data[i] */ } should get auto vectorized and turn into SIMD, at least for the stuffs compiler can recognize.

grave raft
jovial oriole
grave raft
#

ik, i could see you used ai to try to help me

jovial oriole
grave raft
#

I need the gas flow for my engine

#

Fuel and stuff

jovial oriole
#

isnt there online stuff for that already?

grave raft
#

It will make engine run and i could use the same fluid sim to also create sound

grave raft
#

There is one person that made it, but he will not share it

jovial oriole
#

if you want to add me as a friend, you share more about it... I can email it to my friend and see if he gets back to me

#

he knows nothing about engines tho, just math and coding

hexed meteor
#

Anyone know how to generate lightmap UV's for ProBuilder via c# ?

midnight violet
timber flame
#

My project uses URP rendering. My question is although it is URP but my custom frag shader (CG) is OK! It works.
Can URP projects work with CG shaders?!

timber flame
#

Other urp shaders work as well

untold moth
timber flame
#

It is none 🙂

#

but how both of them work

untold moth
#

Or you're misunderstanding something. A confirmation error.

#

Also, shader graph supports both urp and built-in nowadays, if you're assuming based on that.

sage radish
timber flame
timber flame
# untold moth Good question. Maybe they contain both CG and hlsl code? The compiler would only...

For example, there are two shaders.One of them contains hlsl with hlsl files

 SubShader
    {
        Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }

        Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
        Cull Off
        ZWrite Off

        Pass
        {
            Tags { "LightMode" = "Universal2D" }

            HLSLPROGRAM

 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

The other one

  Blend SrcAlpha OneMinusSrcAlpha
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

Both of them have been used in the scene and prefab and they work

sage radish
#

Lit shaders from the built-in RP only become pink in URP because URP is filtering out shaders with incompatible LightMode tags. They could technically render, they just wouldn't render correctly.

timber flame
#

So, I can use custom built-in shaders unlit shaders

delicate mural
#

One of the issues is that the character uses CC, and whenever it moves the held object lags behind terribly and jitters

#

I can't imagine how you can fix that. It's kinda just a disparity between the update loops

#

That's really about it. I would be satisfied with just fixing that for this project, I'm not looking for anything fancy

swift orchid
#

Hi any one know how to setup Google Login? I followed the Documentaion nothing works

midnight violet
swift orchid
#

Social.localUser.Authenticate(OnGoogleLogin) -> sucess is false form anroid device

midnight violet
swift orchid
#

I got it once when run app first time and now I do not get it from my device

midnight violet
#

did you login back then? Or probably denied the access?

#

I would delete the app and install again to see if it triggers once more

swift orchid
#

I delete it many times I got loginned after first build all rest builds is not logining

#

delete app data also is not helping

midnight violet
#

What does Social.Active give you when logged?

swift orchid
#

Looks like the problem it cannot login to google via this line Social.localUser.Authenticate(OnGoogleLogin);

azure meadow
#

Have you ever had an issue where your scene becomes all null characters? 😐

sly grove
#

anyway git revert/git checkout should sort you out

azure meadow
summer knoll
#

Hi all, I have a "controller" class that acts as a proxy for other classes to request things like RequestEndRoom() & RequestRestartRoom()

However, I only want specific classes to be able to call these public methods. Is there some sort of design paradigm I can use to implement that access restriction?

#

I am considering using some sort of unique ID authorization secret that is present on those classes and feeds it to the proxy, which will authorize it

#

Appreciate any point in the right direction!

long ivy
#

only let the authorized classes know about the controller, then. If you made it in a singleton, you have a code smell you're about to double down on with the weird authorization thing

bleak citrus
summer knoll
bleak citrus
#

if you don't have time to do it right, when will you have time to do it again?

#

it sounds like you're trying to create something like C++'s friend

#

If you need to keep the giant singleton, just document that these methods aren't meant to be called willy-nilly

#

but that's kind of true for all methods

summer knoll
#

fair, thanks for the response!

bleak citrus
#

I've bumped into a similar problem before -- I was trying to limit access to class Foo by handing our an intermediate Bar that held a reference to it

#

i also didn't want anyone to be able to construct a Bar on their own for reasons I forget

#

I wound up just ditching that

split wren
#

Hello, I have a quick question - do global properties break GPU instancing? I.e if I want to use a global property (set by Shader.SetGlobalVector(name)), do I have to use UNITY_ACCESS_INSTANCED_PROP and UNITY_DEFINE_INSTANCED_PROP?

#

And one more, when is the value of build-in shader props set? I need to override _Time so we can get more determinism from tests, and overriding it in Update didn't work. Is it even possible, or am I out of luck?

plain crescent
#
    private void CheckWallCollision(Vector3 newPos)
    {
        if (IsColliding(newPos))
        {
            Vector3 adjustPos = AdjustCollisionPosition(newPos);
            transform.position = new Vector3(adjustPos.x, transform.position.y, adjustPos.z);
        }
        else
        {
            transform.position = new Vector3(newPos.x, transform.position.y, newPos.z);
        }
    }

    private bool IsColliding(Vector3 targetPos)
    {
        Vector3 start = transform.position + Vector3.up * (_capsuleHeight / 2 - _capsuleRadius);
        Vector3 end = transform.position + Vector3.down * (_capsuleHeight / 2 - _capsuleRadius);
        //Vector3 direction = targetPos - transform.position;
        //float distance = direction.magnitude;

        return Physics.CheckCapsule(start, end, _capsuleRadius, _collisionMask);
    }

    private Vector3 AdjustCollisionPosition(Vector3 targetPos)
    {
        Vector3 beforeTP = targetPos;

        Vector3 start = transform.position + Vector3.up * (_capsuleHeight / 2 - _capsuleRadius);
        Vector3 end = transform.position + Vector3.down * (_capsuleHeight / 2 - _capsuleRadius);
        Vector3 direction = targetPos - transform.position;
        float distance = direction.magnitude;

        RaycastHit hit = new RaycastHit();
        if(Physics.CapsuleCast(start, end, _capsuleRadius - Physics.defaultContactOffset, direction, out hit, 0.1f /*_collisionMask*/))
        {
            targetPos = hit.point - direction.normalized * _capsuleRadius;
            Debug.Log("Wall has been hit");
        }

        Debug.Log(
            $"\n {beforeTP} <- Where player wants to go : Where player will be going -> {targetPos}\n"
            + $"{start}, {end}, {direction}, {distance}");

        return targetPos;
    }

I am trying to make a CapsuleCast detect walls.

#

Sadly enough the

if(Physics.CapsuleCast(start, end, _capsuleRadius - Physics.defaultContactOffset, direction, out hit, 0.1f /*_collisionMask*/))
        {
            targetPos = hit.point - direction.normalized * _capsuleRadius;
            Debug.Log("Wall has been hit");
        }

Has none of it and doesn't want to detect any type of wall or config I tried to apply to it

#

I have read the Unity Manual (please don't post it here) and I am aware that a Capsule Cast won't detect a collision when it is already overlapping something...

#

But it doesn't overlap anything as there are one 3 colliders in my world and all have a different mask.

#

What am I not seeing?

upbeat path
plain crescent
#

For some odd reason it started working?

upbeat path
#

Distance is zero ?

plain crescent
#

Okay ... nope ... its still broken.
When I head to the wall and get in between the highest and the second lowest it works but when running into the highest wall .... it doesn't work

#

distance is the position I want the character to move to during input - the position its already at.
Meaning that when the player isn't moving the distance will be 0, that is possible right?

upbeat path
#

Direction is also zero, that can't be what you want

plain crescent
#

I'll test again

#

This is just forward movement.
What I find mostly strange is that the capsule collider does get triggered when coming in from the corner of the wall it seems...

#

This is where collision is detected

upbeat path
#

Im guessing there is something flawed in your logic
between these two.
The initial conditions are relatively the same but the results are different

#

at a guess targetpos is incorrect

plain crescent
#
private void HandleMovement()
{
    _velocity += Physics.gravity.y * _gravityScale * Time.fixedDeltaTime;

    CheckGroundCollision();

    if (Input.GetKeyDown(KeyCode.Space)) Jump();

    int horizontal = (int)Input.GetAxisRaw("Horizontal");
    int depth = (int)Input.GetAxisRaw("Vertical");
    Vector2 movement = new Vector2(horizontal, depth).normalized;

    Vector3 nextPosition = transform.position + new Vector3(movement.x, 0, movement.y) * _runSpeed * Time.fixedDeltaTime;
    CheckWallCollision(nextPosition);

    transform.Translate(Vector3.up * _velocity * Time.fixedDeltaTime);
    
    HandlePlayerGFXFlip(horizontal);
}

#

This is where "targetPos" is made but called nextPosition

#

Okay breakthrough

#
private bool IsColliding(Vector3 targetPos)
{
    Vector3 start = targetPos + Vector3.up * (_capsuleHeight / 2 - _capsuleRadius);
    Vector3 end = targetPos + Vector3.down * (_capsuleHeight / 2 - _capsuleRadius);
    return Physics.CheckCapsule(start, end, _capsuleRadius, _collisionMask);
}

I forgot to actually use the thing .... targetPos during my collision check

#

Now I only need to figure out how I can prevent my character from clipping into a wall

#

when moving diagonally

deep jasper
#

how do i work with int[,] in a compute shader?

lament salmon
deep jasper
#

one sec, i'll explain better

#

i actually don't need int, bool is better, as i think rn

#

so, i need to create a bool array inside the compute shader with size(for example) 95x95
then i need to read it from script and loop like this

for (int i = 0; i < 95; i++) {
    for (int j = 0; j < 95; j++) {
        if (myBoolArray[i,j]) {
            // do my code
        } else {
            // do my other code
        }
    }
}```
sage radish
deep jasper
#

oh true

#

sorry for my java issues

lament salmon
#

I don't think you can have a StructuredBuffer<bool> so you'd have to treat them as integers

#

And you have to convert 2D into 1D array/buffer

deep jasper
#

is there a way to do that so i don't mess up indexing

#
computeShader.Dispatch(/**/);
computeBuffer.SetData(boolArray);
for (int i = 0; i <= 95; i++)
{
    for (int j = 0; j <= 95; j++)
    {
        if (myBoolArray[i,j]) {}
    }
}```
#

that's what i currently have, but compute shader part is empty

#

actually... i can just use 1d array with amount of elements = x * y;

#

and then inside the loop split it back to 2 number i need

#

if element index is bigger than x i add 1 to y and go ahead

#

sounds kinda stupid

#

i don't think i know what i'm doing

lament salmon
#

Well you can convert 1D to 2D coordinates something like thiscs int x = index % xSize; int y = index / xSize

deep jasper
#

will i be able to read it into a bool[,] array which is in my c# script?

#

or i should go for 1d in both computeshader and c# script

lament salmon
#

I think that's up to you

#

If you need to write into the buffer from the shader, you need RWStructuredBuffer, not StructuredBuffer

deep jasper
#

actually i don't

#

just need to read from buffer to c#

lament salmon
#

I thought you pass it from C#

#

What's the point of reading it back if it's not going to change inside the shader, is my point

deep jasper
#

buffer has fixed size and is repopulated with true/false's each Dispatch()

#

i don't pass related stuff to shader, only read from it

lament salmon
#

So the shader does write to the buffer?

deep jasper
#

yeah

lament salmon
deep jasper
#

wait

#
private ComputeBuffer buffer = new ComputeBuffer(95 * 95, sizeof(bool));
private bool[] cells = new bool[95 * 95];```
#

i create the buffer, then
computeShader.SetBuffer(kernelId, "cellBuffer", buffer);

#

then each frame i dispatch the compute shader

#

and then buffer.SetData(cells);

#

and after gathering data i loop

for (int i = 0; i <= 95 * 95; i++)
{
    int x = i % 95;
    int z = i / 95;
    if (cells[i]) {}   
}```
sage radish
deep jasper
#

so i'd better place 4

#

why does bool have size 4 tho

sage radish
#

You also need to change the array element type to be 4 bytes.

sage radish
deep jasper
#

so what do i do with array then

#

if i can't use bool

sage radish
#

Change it into an int array, that's 4 bytes.

deep jasper
#

so in computeshader i also use int instead of bool?

#

RWStructuredBuffer<bool> cells;

#

or it's gonna work anyway

sage radish
#

Up to you. I would use int just to make it less confusing.

deep jasper
#

hlsl sucks xD

sage radish
#

If you want to save memory, you can use a RWByteAddressBuffer. Then you can even go as far as storing 1 bit per bool, with some bitwise operations in the shader.

deep jasper
#
computeShader.Dispatch(kernelId, Mathf.CeilToInt(95 * 95 / 16), 1, 1);```
#

mafs

#

lez go

hardy jacinth
# deep jasper why does bool have size 4 tho

Because the basic unit of operation on a GPU is a 4-component vector. That's why GPUs are fast — they can do in one cycle what takes 40 on a CPU. So on GPU you can for example add two 4-component vectors just as if you did one operation, essentially performing computation for free.

deep jasper
#
int x = id % 95;
int z = id / 95;
float3 coords = float3(x * 64 + 32, 1, z * 64 + 32);
float4 CameraDistances0 = float4(
        dot(frustumPlanes[0].xyz, coords) + frustumPlanes[0].w,
        dot(frustumPlanes[1].xyz, coords) + frustumPlanes[1].w,
        dot(frustumPlanes[2].xyz, coords) + frustumPlanes[2].w,
        dot(frustumPlanes[3].xyz, coords) + frustumPlanes[3].w
    );
float4 CameraDistances1 = float4(
        dot(frustumPlanes[4].xyz, coords) + frustumPlanes[4].w,
        dot(frustumPlanes[5].xyz, coords) + frustumPlanes[5].w,
        0.0f,
        0.0f
    );
if (all(CameraDistances0 >= -2) && all(CameraDistances1 >= -2))
{
    cellBuffer[id] = 1;
}
else
{
    cellBuffer[id] = 0;
}```
#

am i dumb

#

it always returns 0

#

first coord should be 32/1/32 cuz id = 0, which means x = 0 and z = 0

#
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);
float[] planeNormals = new float[planes.Length * 4];
for (int i = 0; i < planes.Length; i++)
{
    planeNormals[i * 4 + 0] = planes[i].normal.x;
    planeNormals[i * 4 + 1] = planes[i].normal.y;
    planeNormals[i * 4 + 2] = planes[i].normal.z;
    planeNormals[i * 4 + 3] = planes[i].distance;
}
computeShader.SetFloats("frustumPlanes", planeNormals);```
#

that's how i pass planes

hardy jacinth
#

probably all(CameraDistances0 >= -2) is not doing what you think it is or maybe not

deep jasper
#

i have the same thing in another shader tho

hardy jacinth
#

maybe first try checking any() for cameraDistances0 only and see if that even works

#

or rather try to stop succesively and see on which stage it breaks

deep jasper
#

i just

#

i % 2 == 0

#

🙂

#

gonna have chess now ig

#

well

#

it's definitely not the shader error

#

buffer.SetData(viewedCells);

#

it returns a set of 0's

hardy jacinth
#

what kind of buffer are you using?

#

RWBuffer, RWStructuredBuffer or what?

deep jasper
#

RWStructuredBuffer<int> cellBuffer;

#

in the method i just do cellBuffer[id] = 1; rn

#

then reading data and debug log every value

#

and it's all 0

hardy jacinth
#

do you have proper #pragma target maybe?

deep jasper
#

i only have #pragma kernel

#

what is target

#

i don't think it should matter since my other compute shader works well without target

hardy jacinth
#

different compute models support different types, so I had an idea that maybe by default in your case RWStructuredBuffer is not supported. iirc default compute model is 3.0 or so

deep jasper
#

it is supported for my other grass compute shader

#

#pragma kernel CSMain

RWStructuredBuffer<float3> positions;

hardy jacinth
deep jasper
#

#pragma target 5.0 is what i go for ig

hardy jacinth
#

yeah, it should cut it

#

changing buffer type to float4 maybe?

deep jasper
#

nah, still zeros

deep jasper
#

actually why do i need float4

#

just float seems alr

hardy jacinth
#

dunno, from what I know shader compiler bugs are discovared on a daily basis, wouldn't be weird if there was an aundocumented edge case xd

hardy jacinth
deep jasper
#

wait

hardy jacinth
#

maybe when you defined RWStructuredBuffer<int> it's 4 times smaller than <int4> and <float4> so you index out of bounds

deep jasper
#

can RWStructuredBuffer<int> be read to int[]

hardy jacinth
#

unsure

deep jasper
#

at least in c#

#

might be a shader error yeah

#

what do i put instead of int[] if i go for float4?

hardy jacinth
#

idk if you would get error in shader, it can depend, shader you even happily try to read and write garbage, in this term it's pretty similar to C++ and C

#

Vector4?

deep jasper
#

let's see

#

stride of buffer is 16 right?

#

4*4(4 floats)

hardy jacinth
#

btw I don't really know compute shaders and their API but I did some regular shader shennanigans, but in your case I'm just throwing ideas

deep jasper
#

it returned (0000)

#

😉

hardy jacinth
#

kekw

#

show the whole compute shader* and how you call it from unity

deep jasper
#
private int[] viewedCells = new int[16 * 16];
private ComputeBuffer buffer;
[SerializeField] ComputeShader computeShader;
private int kernelId = 0;
private Camera cam;

private void Start()
{
    cam = Camera.main;
    buffer = new ComputeBuffer(16 * 16, 4, ComputeBufferType.Structured);
    kernelId = computeShader.FindKernel("CSMain");
    computeShader.SetBuffer(kernelId, "cellBuffer", buffer);   
    player.SetActive(true);
}

public void UpdateVisibility()
{
    computeShader.SetFloats("camPosition", cam.transform.position.x, cam.transform.position.y, cam.transform.position.z);
    Plane[] planes = GeometryUtility.CalculateFrustumPlanes(cam);
    float[] planeNormals = new float[planes.Length * 4];
    for (int i = 0; i < planes.Length; i++)
    {
        planeNormals[i * 4 + 0] = planes[i].normal.x;
        planeNormals[i * 4 + 1] = planes[i].normal.y;
        planeNormals[i * 4 + 2] = planes[i].normal.z;
        planeNormals[i * 4 + 3] = planes[i].distance;
    }
    computeShader.SetFloats("frustumPlanes", planeNormals);
    computeShader.Dispatch(kernelId, Mathf.CeilToInt(16 * 16 / 16), 1, 1);
    buffer.SetData(viewedCells);
    for (int i = 0; i <= 16 * 16 - 1; i++)
    {
        int x = i % 16;
        int z = i / 16;
        Debug.Log(viewedCells[i]);
    }
}```
#
#pragma kernel CSMain

RWStructuredBuffer<int> cellBuffer;
float4 frustumPlanes[6];
float3 camPosition;

[numthreads(16, 1, 1)]
void CSMain(uint id : SV_DispatchThreadID)
{
    int x = id % 16;
    int z = id / 16;
    float3 coords = float3(x * 64 + 32, 1, z * 64 + 32);
    float4 CameraDistances0 = float4(
            dot(frustumPlanes[0].xyz, coords) + frustumPlanes[0].w,
            dot(frustumPlanes[1].xyz, coords) + frustumPlanes[1].w,
            dot(frustumPlanes[2].xyz, coords) + frustumPlanes[2].w,
            dot(frustumPlanes[3].xyz, coords) + frustumPlanes[3].w
        );
    float4 CameraDistances1 = float4(
            dot(frustumPlanes[4].xyz, coords) + frustumPlanes[4].w,
            dot(frustumPlanes[5].xyz, coords) + frustumPlanes[5].w,
            0.0f,
            0.0f
        );
    if (all(CameraDistances0 >= -2) && all(CameraDistances1 >= -2))
    {
        cellBuffer[id] = 1;
    }
    else
    {
        cellBuffer[id] = 0;
    }
}```
#

16*16 everywhere is temporary

#

currently shader is just

#pragma kernel CSMain

RWStructuredBuffer<int> cellBuffer;
float4 frustumPlanes[6];
float3 camPosition;

[numthreads(16, 1, 1)]
void CSMain(uint id : SV_DispatchThreadID)
{
    cellBuffer[id] = 1;
}```
hardy jacinth
#

emmm, are you testing it in editor or runtime? Start doesn't run after domain reload just FYI

deep jasper
#

in editor

#

but i delay the UpdateVisiblity() until everything is set up

hardy jacinth
#

Move the code from start to UpdateVisibily just before you do all your shader shennanigans, see if it helps

lament salmon
#

You are calling buffer.SetData after dispatching? 🤔

deep jasper
#

OH

#

SHIT

hardy jacinth
#

lol

deep jasper
#

IT'S SETDATA

#

NOT GETDATA

lament salmon
#

And I don't see you reading from the buffer anywhere

#

There we go lol

deep jasper
#

one freaking letter

#

lez go it works

#

i spent an hour on fixing one letter

#

fps from 5 to 75 ez

#

now i have terrain culling xD

#

i need to rewrite culling a bit, but it's already cool

deep jasper
#

on target world size fps is still too low

#

i need some other way to manipulate terrains

lament salmon
deep jasper
#

unity terrains

lament salmon
#

And what exactly do you mean with manipulate

deep jasper
#

since i thought unity terrains are cool

deep jasper
#

it's LODded as hell, but terrains are not billboards, you can easily see they are meshes

#

so my initial idea was to load every chunk/cell and test the performance

#

fps was less than 5

#

even tho unity tries it's best to downscale terrain meshes

#

then i decided to try culling terrains which are not seen by disabling terrain component

#

i wrote this cool compute shader which allowed me to have 120 fps on world size 16x16(each chunk is 64x64, so total world size is 1024x1024)

#

but if i scale it to have beautiful distant mountains far away, fps gets to like 20

#

rn world is nearly skyrim size(36,9km^2 compared to 37,7km^2 in skyrim)

#

and well, most of terrains are actually culled

#

but they are still 9k+ individual objects with scripts attached, and also with child gameobjects(grey cubes), which represent houses/castles/rocks on each cell

#

right now i have no clue what to do

#

is there a way to build just a single terrain out of those small cells or at least using all 9k heightmaps

#

and if it's possible, how do i make LOD for it

lament salmon
#

I'm not too familiar with those though - I ended up making my own terrain system that uses compute shaders for generation and a custom lod system

deep jasper
#

already played with those params on my chunks, but results are meh

lament salmon
#

You can turn on Shaded Wireframe in the scene view to see how the terrain LODs work

deep jasper
#

oh i see yeah

#

terrain lods itself based on distance to the player

#

well, that means i now need to find a way to create one terrain out of several small ones

plucky terrace
#

MVP Architecture Enforcement : Use Assemblies or Just Use Namespaces ?

bleak citrus
gilded sonnet
#

Hey guys, I'm having an issue where fields marked as [SerializeField] of a specific type (ItemPresenter) don't show any class in the picker, even though there are instances of this type in the scene. I've verified everything I possibly could, but nothing seems to work. I suspect there might be some corrupted data because this issue started after a branch merge. Any ideas on what might be causing this?

What I've already done:

  • Checked if the class and file names are correct
  • Reimported the project (initially thought the issue might be related to my machine, not the repo)
  • Reimported the library (same reason as above)
  • Regenerated meta files for ItemPresenter.cs and the root prefab, just in case
  • Compared the scenes between working and non-working commits

The picker for this component doesn't show any classes, nor do I can drag and drop directly either.

sly grove
#

It will show assets of that type, if any exist

#

is ItemPresenter a MonoBehaviour?

gilded sonnet
sly grove
#

A ScriptableObject?

#

What is it

gilded sonnet
#

yup, it's a MonoBehaviour. This is old reference which is still present and works correctly - but now, after the merge which was like 2 weeks ago, i just can't change it

sly grove
gilded sonnet
#

There are bunch of items already setuped on the scene, and they are just not picked-up by the select picker window. But if the context (scene stage) changes to the prefab stage, it's working correclty. That's weird notlikethis

sly grove
#

because that's not possible

#

not at edit time anyway

gilded sonnet
#

yeah sure, I could've probably better phrase that. Just for context I'm not a begginer I know how things are working - just in this case there must be some corrupted data which is causing the issue with the picker window and I can't figure it out

#

I've checked the scene diff, but I might do it again as in prefab stage everything seems to work fine

#

And as I mentioned there are already some hard referenced item presenters which persisted in those prefabs yamls

gilded sonnet
#

Update.. it's broken even for other types likes basic Transform.. but not all of them. Some objects are detected correctly

gilded sonnet
gilded sonnet
#

It started working.. for no reason. I just iterate over different pickers to check which were working and then.. it just started work.
// Edit: Odin Inspector has a bug and version 3.3.1.3 fixed this

tired stirrup
#

Hello, anyone has experience in implementing Google Tag Manager in Unity for Mobile platforms? I have followed a single one tutorial that i found, but with no success. (I dont know if this is the right place do ask this)

proven willow
#

I need help with saving my data. My game's architecture relies on lists of Scriptable Objects with lists of integers in them. I am trying to convert them into JSON, but all I'm getting is their reference IDs. The list I'm displaying here, for example, is a list of object states in their scenes. Each of these objects is holding a list of objects with integers in them, but the only thing being saved is their IDs. Will the data be saved and loaded properly with nothing but the references? If not, what should I do?

tall ferry
sly grove
#

Or use a different serializer

dim moth
#

I think thats the one I use.

proven willow
boreal mica
#

Ok so stumbled upon an issue. Everything was working fine, assed a couple items from assets that where already in the project from before. Now even with Networking disabled just placing a character in the scene it acts very wierd. I included a video here.
https://youtu.be/HXMrWUjrSSo
Anyone ever seen this issue before?

This is the issue Im having right now. Using Unities Starter Assests, the new input system. I have turned off the aimcamera and the aimscript and even turned off the Network Manager object and scripts.

▶ Play video
gilded sonnet
versed gulch
#

Hey lads! Does anyone here have a working GPS-system/script for android/IOS? Can't get mine to update the location of the device :/

sick flame
#

Guys, Can DXC shader compiler be used in Unity?

warped flicker
#

Right? Can someone confirm or deny, I decided to check and cant find it XD I remember getting it there...

#

Maybe it's installed manually "com.unity.nuget.newtonsoft-json": "3.0.2"

stuck plinth
sick flame
dim moth
stuck plinth
hardy jacinth
#

I'm trying to display my custom serializable class in the inspector and it's not working

#
[Serializable]
public abstract class ShaderPreset {
    protected abstract RaymarchingShaderGenerator CreateProcessorForScene(SdfScene scene);
    public string MainShaderForScene(SdfScene scene) => CreateProcessorForScene(scene).MainShader();
}

[Serializable]
public class BirpShaderPreset : ShaderPreset {
    public ShaderInclude[] additionalIncludes;
    public string[]        additionalDefines;

    protected override RaymarchingShaderGenerator CreateProcessorForScene(SdfScene scene) => new BuiltInGenerator(scene);
}
#

I added a public ShaderPreset preset = new BirpShaderPreset() field in another class and it doesn't display

#

I expected a foldout with 2 fields, one for shader include assets and another one for strings to display

stuck plinth
#

ScriptableObjects don't need to have [Serializable] either

hardy jacinth
#

oh, sorry, I have modified code since, disregard the scriptable object, I am editing the message to reflect how I would like it to look like

#

it's a plain C# object

stuck plinth
#

ahh in that case polymorphism and unity serialization aren't really compatible, you need to specify the concrete type for the field

#

it'll be serialized as whatever the type of the field is, and since it's abstract you can't serialize that

hardy jacinth
#

oh no

#

isn't there any workaround?

#

I already have a custom inspector for the type, so If I could even write a generic code just to handle the abstract base class

stuck plinth
#

it needs to be serialized indirectly for polymorphism to work, so you can either make it a ScriptableObject, or you can use SerializeReference

#

that said you might just find the simplest solution is to put all the possible fields in a single type and conditionally hide them in your custom inspector depending on a type dropdown or something like that

hardy jacinth
#

unfortunately that isn't an option, because I need the ability for users to create their own classes derived from ShaderPreset

#

I tried your idea for using SerializeReference, but the only thing displayed in the inspector is a label

#

for the following:

public class PresetTest : MonoBehaviour {
    [SerializeReference] public AbstractPreset preset;
    void OnEnable() { preset = new Preset(); }
}

[Serializable]
public abstract class AbstractPreset {
    public void DoSomething() => Debug.Log("test preset");
}



[Serializable]
public class Preset : AbstractPreset {
    public ShaderInclude[] additionalIncludes;
    public string[]        additionalDefines;
}
stuck plinth
#

SerializeReference means you need to provide your own property drawer yeah, it doesn't have one by default

hardy jacinth
#

hmm, could I provide a generic one for the AbstractPreset that automatically generates all necessary fields?

stuck plinth
#

if you have a way of determining those fields sure, once you go down this route it's all up to you to decide what you show

dusty wigeon
hardy jacinth
dusty wigeon
#

If it is null, you have nothing showing.

stuck plinth
#

yeah, you need to provide some way for the user to create an instance of something

hardy jacinth
dusty wigeon
#

OnEnable is not called ?

hardy jacinth
#

ok, yeah, it started working after enabling the game object (facepalm) and adding [ExecuteAlways]

#

thank you very much

#

anything I should be aware of when using SerializeReference?

#

gotchas etc

regal lava
#

renaming reference tends to break serialization

#

similarly for renaming classes/interfaces

#

I think people said you can do formally serialized as attribute but I dont think that has worked for me.

dusty wigeon
#

You can use Moved attribute

hardy jacinth
#

oh, good to know

deep jasper
#

how much cpu does an empty component consume