Call a function in a WindowEditor script from another script

45 Views Asked by At

I am using Minions Art grass system, I want to be able to call and regenerate the current mesh on line 301 from another script, I cant find a way to interact with anyhting on this script from another one, so when I generate my mesh, it generates the grass

https://www.patreon.com/posts/grass-system-urp-83683483

Im also using sebastian lagues procedural generation tutorial, using the falloff map

https://www.youtube.com/watch?v=wbpMiKiSKm8&list=PLFt_AvWsXl0eBW2EiBtl_sxmDtSgZBxB3

Im adding a pastebin link so you all can see the code:

[https://pastebin.com/rqcTdUyQ][1]
1

There are 1 best solutions below

1
derHugo On

A bit hard to tell from your question and a huge code snippet.

In general: By making the method public static so it can be called from anywhere (assuming the wrapping type is visible of course) ... might need to refactor a bit to not rely on any instance fields but rather take parameters (using ref/out where required)

I didn't count the lines but I will just assume you are referring to CreateNewGrassObject (it's the only non-drawer related thing you included in your question)

So I would convert this to

public static GrassComputeScript CreateNewGrassObject()
{
    var grassObject = new GameObject();
    grassObject.name = "Grass System - Holder";
    var grassCompute = grassObject.AddComponent<GrassComputeScript>();

    // Why is this not simply done in the GrassComputeScript by default on field declaration?
    grassCompute.SetGrassPaintedDataList = new List<GrassData>();

    return grassCompute;
}

all other local values are available then as well anyway via e.g.

var newGrass = GrassPainterWindow.CreateNewGrassObject();
var gameObject = newGrass.gameObject;
var grassData = newGrass.SetGrassPaintedDataList;

Since it contains no editor specific code, you might even consider to move this out into a helper class like

public static class GrassTools
{
    public static GrassComputeScript CreateNewGrassObject()
    {
        ...
    }
}

so it could be available even in a build.