0
\$\begingroup\$

I would like to apply a texture to a plane. I want to assign a certain offset and tiling to it.

The material has a Tiling and Offset option. However, I don't want to do it on the Material level.

Why? The material has 5 different Tiling / Offset options. To try out different tiling / offsets, I have to change all 5 of them. This does not seem like a good workflow to me.

Instead, I would to apply the tiling / offset on the game object.

Is that possible?

Edit: I think what I actually need is a possiblity to change the UV of a mesh in a flexible way. I think I have found it in ProBuilder's UV options.

enter image description here

enter image description here

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Just write a script that iterates over the material's texture properties and updates their tiling and scaling to match a master value.

using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class MaterialScaler : MonoBehaviour
{
#if UNITY_EDITOR
    public Vector2 tiling = new Vector2(1, 1);
    public Vector2 offset = new Vector2(0, 0);

    private void OnValidate() {
        var properties = new List<int>();
        foreach(var material in GetComponent<Renderer>().sharedMaterials) {
            material.GetTexturePropertyNameIDs(properties);
            EditorUtility.SetDirty(material);

            Undo.RecordObject(material, "Change Shared Tiling/Offset");

            foreach(var id in properties) {
                material.SetTextureScale(id, tiling);
                material.SetTextureOffset(id, offset);
            }
        }
        AssetDatabase.SaveAssets();
    }
#endif
}

If you really want to pretend this is part of the responsibilities of the GameObject class itself, you could always write it as an extension method:

public static class GameObjectExtensions {
    public static void TransformAllTextures(this GameObject gameObject, Vector2 tiling, Vector2 offset) {
        var properties = new List<int>();
        foreach(var material in gameObject.GetComponent<Renderer>().sharedMaterials) {
            material.GetTexturePropertyNameIDs(properties);

            foreach(var id in properties) {
                material.SetTextureScale(id, tiling);
                material.SetTextureOffset(id, offset);
            }
        }
    }
}

You can now call this with:

someGameObject.TransformAllTextures(tiling, offset);
\$\endgroup\$
11
  • \$\begingroup\$ Thank you, but is there no such property for the gameobject itself? If not, do you have any idea why not? \$\endgroup\$
    – tmighty
    Commented Oct 16, 2021 at 13:57
  • 1
    \$\begingroup\$ Because GameObjects are just containers for components, and so they have nothing whatsoever to do with materials. It would be absurd to put a material tiling property on the base GameObject class when that GameObject could easily be a camera or an invisible trigger volume or a manager etc. that has no materials associated with any of its components. \$\endgroup\$
    – DMGregory
    Commented Oct 16, 2021 at 14:00
  • 1
    \$\begingroup\$ I've added an example using an Extension method if you'd like to pretend that this is the GameObject's job. 😉 \$\endgroup\$
    – DMGregory
    Commented Oct 16, 2021 at 14:08
  • \$\begingroup\$ Thank you. I have added a screenshot to my post. Is my idea so unusual? In my screenshot you see how I have 2 planes, and I would like to have the same material on both, but on one of them, the tiling should be different. Is that so uncommon? \$\endgroup\$
    – tmighty
    Commented Oct 18, 2021 at 17:11
  • 1
    \$\begingroup\$ If you meant to ask "How can I use the same material on two objects, and use a different tiling on one of them", then that's a different question than the one you asked. It sounds like you may be interested in Tiling of a material, independent of its size. You can also accomplish this with a script like I showed above, simply by making it instantiate a copy of the material for each new tiling rate you want to use. \$\endgroup\$
    – DMGregory
    Commented Oct 18, 2021 at 17:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .