Change imported image settings via script - c#

I am trying to make an editor script to set the import settings for my images. I don't want to do this manual since I need to import hundreds of images.
So I want to set edit the default import settings.
I tried the following:
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
[InitializeOnLoad]
public class EditorSettings : Editor {
private static TextureImporter CustomImporter;
static EditorSettings()
{
CustomImporter.npotScale.None; // see below for error.
}
}
the error I get is the following:
Member 'TextureImporterNPOTScale.None' cannot be accessed with an instance reference; qualify it with a type name instead
How do I do this? (it has something to do with how unity lets me acces the properties.)
And is this even the correct way to change the import settings for images?
Let me know if anything is unclear so I can clarify.

How do I do this? And is this even the correct way to change the
import settings for images?
No. This is not how to change the import settings of images. To change the settings of an imported image, you have to create an Editor script that derives from AssetPostprocessor then change the image settings in the OnPostprocessTexture function which will be called when the image has finished importing. The image is changed with the TextureImporter class.
public class PostprocessImages : AssetPostprocessor
{
void OnPostprocessTexture(Texture2D texture)
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.npotScale = TextureImporterNPOTScale.None;
}
}

Related

Monobehaviour scripts in the file, or their names don't match the file name on newly made script

Why all my newly created script always have this error? Even though i had put in my intended code, all my script always show like this.
my code is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Testing : MonoBehaviour
{
public GameObject obj;
// Start is called before the first frame update
void Start()
{
obj = GetComponent<GameObject>();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D collision)
{
collision.transform.position = obj.transform.position;
}
}
Welcome to Stackoverflow!
This issue is mostly caused when you create a script outside the Unity Editor, i.e. in your IDE or editor of choice. Whenever you want to create a script, you need to create it inside the Editor, by right-clicking in your project folder and selecting "New C# Script".
If you do create your script inside Unity, you need to make sure that the class name (in your case, "Testing") is the same as the script file name.
Regardless of whether these solutions work for you, please make sure to search for answers before posting, as there are multiple duplicate issues in the Unity forums.

Setting up a UI slider to control post processing effects?

As the title says, I'm trying to set up a UI slider so the player can adjust some of the post processing settings (specifically the exposure and temperature) while the game is running.
To bring you up to speed:
I'm using version one of the post processing stack, as found here:
https://assetstore.unity.com/packages/essentials/post-processing-stack-83912
I'm very new to C#; I'm at the "Frankenstein" stage of learning how to code (following tutorials and modifying what works until it breaks or does what I'm trying to accomplish).
I figure my best shot is to try to adapt what I learned from this tutorial on creating an audio volume slider: https://www.youtube.com/watch?v=YOaYQrN1oYQ&t=122s
This is the code I've cobbled together so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BrightnessSlider : MonoBehaviour {
public void SetBrightness (float brightness)
{
Debug.Log(brightness);
}
}
Specific issues I'm having at specific points in the tutorial:
2:07 For the function of the slider, the tutorial sets up a dynamic float that matches the custom method (SetVolume) they specify. When I try setting up my own function with a custom method (SetBrightness), I can't find it. I'm also not sure if I need to set a different object instead of the canvas for this step.
3:47 In the tutorial they expose a parameter for the volume so that it can be manipulated through script, but I don't know what the equivalent of that would be for post processing.
For the record, I was able to follow this tutorial to create my own audio slider and got it working with no problems.
One last thing: I opened up the script for the post processing profile and found the variable type I think I'd need or would at least be somewhat relevant: ColorGradingModel, but I honestly have no idea what to do with this information.
Update July 09, 2018


I've since been looking over #Nol's code and had someone else look at it and help me out with it. At the moment, the slider's functionality(not sure if that's the correct terminology but that's what I've been sticking with) is set up through the On Value Changed field in the inspector , but it's not actually driving/changing the brightness values. I had someone else (who's far more qualified than I am) look at it with me. It seems like it should work the way they've set it up, but something is getting lost in translation between the method and the slider.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
using UnityEngine.UI;
public class BrightnessSlider : MonoBehaviour
{
public Slider slider;
public PostProcessingProfile Default;
private ColorGradingModel cgm;
private void Start()
{
//I haven't been able to get this to not return some sort of error,
//and I'm not even sure of its usefulness.
//I've been keeping it commented out for the most part.
Default.profile.TryGetSettings(out cgm);
}
public void SetBrightness(float brightness)
{
ColorGradingModel.Settings settings = cgm.settings;
settings.basic.postExposure = brightness;
cgm.settings = settings;
Debug.Log("Brightness is: " + brightness); //For testing purposes
}
}
It seems like you have the core of what you need to know for changing your settings down. That's good.
You'll need a few simple changes:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing; //How you'll access PPV (Post Processing Volume) models and settings
public class BrightnessSlider : MonoBehaviour {
PostProcessingVolume ppv; //You can make this public to set in inspector
ColorGradingModel cgm; //can use ppv.profile.TryGetSettings(out cgm) in Start()
public void SetBrightness (float brightness)
{
cg.[setting you want to change].value = brightness;
}
}

Creating a Scriptable Object in the Unity Editor

So apparently i suck at listening at my university, because i can't figure this out, not even with google...
How do you create a scriptable object in the editor? I have the project open, it looks like this:
Click the Create button as if you wanted to create a folder or C# script or anything.
Select the ScriptableObject from the popup menu.
Get this panel and finalize the object after selecting the script for it.
The problem is: i don't have the ScriptableObject button. I have a script that is a scriptable object (to make sure i even copied the one from the project of the university). I restarted Unity, i checked if there were any packages installed (there werent) and i googled quite a bit. But i just can't seem to get this working...
Is there anything i have to install or add first?
Thanks in advance!
You need another script to add the button which will create an instance from that scriptable object. something like that
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MakeScriptableObject {
[MenuItem("Assets/Create/My Scriptable Object")]
public static void CreateMyAsset()
{
MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();
AssetDatabase.CreateAsset(asset, "Assets/NewScripableObject.asset");
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}
}
You can check this Introduction to Scriptable Objects tutorial on unity website.
I can't comment so just place it like answer:
Don't forget to use UnityEditor.AssetDatabase.GenerateUniqueAssetPath coz simple AssetDatabase.CreateAsset can erase your data:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MakeScriptableObject
{
[MenuItem("Assets/Create/My Scriptable Object")]
public static void CreateMyAsset()
{
MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();
string name = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/NewScripableObject.asset");
AssetDatabase.CreateAsset(asset, name);
AssetDatabase.SaveAssets();
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
}
}

In Visual Studio C# project do files aware the other file content without putting a using statement?

Say If we use two separate files called a.cs and b.cs in a C# project using VISUAL STUDIO, my question is does one file aware of the other WITHOUT putting a using statement about the other file. ie In the file a.cs can we use a class that is already defined in b.cs but not putting a using b.cs; statement in the beginning of the file?.When we compile altogether will the project know each file content and won't raise any error?
I guess you are on the wrong track here. Files don't interact with each other. But the classes do. Namespaces are used to refer to the class that are meant to be used. You can change the file name to anything several times, it won't affect your project. Moreover you can put many classes inside the same file name under the same namespace and you won't have to use using.
Just consider this scenario, Namespace are the area code, and the phone numbers are the classes. Being in the same area already, you don't have to use the area code to call a different number that exists in the same area. But if you are dialling a number outside your area, you would want to use the area code. Basically by adding area code(namespace) infront of the number, you are applying using to refer to the other number(class). Hope you got the idea.
Edit: Explaining programmatically
Suppose this is your Area
using something;
using someotherthing;
namespace MyMainNamespace
{
private class MyMainClass
{
private void blahblah { ... }
}
private class ClassABC
{
private void blahblah { ... }
}
private class ClassXYZ
{
private void blahblah { ... }
}
}
See, in the above example, to interact with the MyMainClass, ClassABC & ClassXYZ. you don't have to use using MyMainNamespace;. Because they all lie in the same area MyMainNamespace. But there exists a class in another namespace like shown below:
using something;
using someotherthing;
namespace SubNamespace
{
public class SecondaryClass
{
public void apple{ ... }
}
}
If you want to access SecondaryClass which lies in SubNamespace(different area) you would have to use using SubNamespace; in your main area. Like:
using something;
using someotherthing;
using SubNamespace; //add the namespace
namespace MyMainNamespace
{
private class MyMainClass
{
private void blahblah {
...
// Now you can use methods & functions that exist in `SecondaryClass`
SecondaryClass secondary = new SecondaryClass();
secondary.apple();
....
....
}
}
}
Hope this is enough to get the idea by now
Also, it doesn't matter that these namespace(MyMainNamespace & SubNamespace) lies in the same file or different file. You NEVER REFER TO THE FILENAME(filename.cs) by applying using. You ALWAYS REFER TO THE NAMESPACES.
If the C# code in a.cs and b.cs are inside the same namespace, then no using statement should be needed. If the 2 cs files use different namespaces, then you will have to put a using statement for the namespace of the code you want to reference.

ToolBoxBitmap Attribute does not work in my component

I have a Windows Form application. I've added a class to my project named MyLibrary.cs and I inherited this class of System.ComponentModel.Component and I've also added a bitmap image with its build action set to EmbeddedResource to my resources folder, but when I write the code below and build the project my customized icon does not change when I want to drag and drop my component to my Form. Would you please help me?
I've checked a lot of links like http://msdn.microsoft.com/en-us/library/system.drawing.toolboxbitmapattribute.aspx and How to find the elusive ToolboxBitmap icon but none of them worked for me!
Here is my code:
MyLibrary.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
internal class resfinder
{
}
namespace MyForm
{
[System.Drawing.ToolboxBitmap(typeof(resfinder),"MyForm.Bitmap1.bmp")]
public class MyLibrary:System.ComponentModel.Component
{
}
}
You should look here it says:
The bitmap must be 16x16
If you specify both a Type and a string, the control searches for an image resource with the name specified by the string parameter in the assembly containing the type specified by the Type parameter
So make sure your bitmap is located in the same assembly as "resfinder" and also put make sure in properties under build actions it is set as an Embedded Resource

Categories