Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animate UV Offset in Unity Animation Timeline
#6
(05-26-2023, 09:50 AM)_Aka_ Wrote:
(05-25-2023, 10:05 PM)sam_bond Wrote: I wrote the following script to try to control the value, but I seem to be getting an error saying that MissingComponentException, there is no Meshrender attached to the Game Object. 

Code:
using UnityEngine;

public class MaterialOffsetController : MonoBehaviour
{
    public float vOffset;
    public GameObject river;
    public int materialIndex;

    private MeshRenderer meshRenderer;

    private void Start()
    {
        // Get the MeshRenderer component from the specified GameObject
        meshRenderer = river.GetComponent<MeshRenderer>();
    }

    private void Update()
    {
        // Update the material's UVOffset.y value based on the vOffset value
        Material[] materials = meshRenderer.materials;
        if (materialIndex >= 0 && materialIndex < materials.Length)
        {
            Material material = materials[materialIndex];
            Vector2 offset = material.GetTextureOffset("_MainTex");
            offset.y = vOffset;
            material.SetTextureOffset("_MainTex", offset);
        }
    }
}

I am setting the script on a empty parent game object and selecting the Curvy Spline Generator as the GameObject. I am pretty sure I am not access the property value correctly.

You are accessing the wrong things. The script needs to modify the module, which will then be used to generate the objects. Your script tries to modify the  generated objects. Here is an example of such script

Code:
using FluffyUnderware.Curvy.Generator;
using FluffyUnderware.Curvy.Generator.Modules;
using UnityEngine;

[ExecuteAlways]
public class ApplyUVOffset : MonoBehaviour
{
    public BuildVolumeMesh VolumeMesh;
    public int MaterialIndex;
    public float UvOffsetY;

    private void Update()
    {
        if (VolumeMesh != null && VolumeMesh.MaterialSettings.Count > MaterialIndex)
        {
            CGMaterialSettingsEx materialSettings = VolumeMesh.MaterialSettings[MaterialIndex];
            materialSettings.UVOffset = new Vector2(
                materialSettings.UVOffset.x,
                UvOffsetY
            );
        }
    }
}

This script should be attached to a game object in your Unity scene. You'll need to set the VolumeMesh (your module), MaterialIndex (0 for the default material), and UvOffsetY properties in the Inspector. The script will then adjust the UV offset of the specified material every frame.

Did this help?

This worked perfectly, thank you so much for pointing me in the right direction.
Reply


Messages In This Thread
RE: Animate UV Offset in Unity Animation Timeline - by sam_bond - 05-26-2023, 10:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Additional offset to Follow behaviour Ayyappa 3 7 04-30-2024, 01:35 PM
Last Post: _Aka_
Information New free asset: Converter For Unity Splines _Aka_ 5 19 04-25-2024, 11:11 AM
Last Post: _Aka_
  Using Unity's SplineContainer in Curvy Splines dlees9191 3 15 02-26-2024, 09:49 AM
Last Post: _Aka_
  Unity 2021.2 Overlay System nehvaleem 5 17 12-15-2023, 10:09 AM
Last Post: _Aka_

Forum Jump: