I am trying to make title screen when i press a button. It goes when i press the button game stops
with Time.timeScale = 0f; and I am trying to do is when I click button time stops and two buttons shows up for quit and resume and I am trying to do when I press the button it sets show = true; in script for Menu button. This work but when I type script for button "resume" witch is:
private void Awake()
{
gameObject.SetActive(false);
}
private void Update()
{
show();
}
private void show ()
{
if (show = true)
{
gameObject.SetActive(true);
}
}
But there is an Error: Cannot assign to show because it is a method group.
I don't know what this mean and how I can repair my code.
it would be great if you made bool
here some example of your code
public bool show;
private void Awake()
{
gameObject.SetActive(false);
}
private void Start()
{
show = false;
gameObject.SetActive(false);
}
private void gameobject()
{
if (show = true)
{
gameObject.SetActive(true);
}
else(show = false){
gameobject.SetActive(false);
}
}
Deleted show() and replaced update() with Start()
Useful information about bool
https://docs.unity3d.com/ScriptReference/30_search.html?q=bool
source: unity documentary
I have a TextArea that I would like to be able to append characters or words to over a period of time. I use Timer from java.util and when I run application in Eclipse everthing works ok, but when I export application into .jar I have performance issue.
Here is video from Eclipse:
http://pl.tinypic.com/r/4ftw1f/8
Here is .jar:
http://pl.tinypic.com/r/6zmoon/8
And code:
#FXML
private TextArea textarea;
public void start(KeyEvent keyEvent)
{
if (keyEvent.getCode() == KeyCode.ENTER)
{
new Timer().schedule(
new TimerTask() {
int i;
#Override
public void run() {
textarea.appendText("hey" + i + "\n");
i++;
}
}, 0, 500);
}
}
Your code has threading issues: in Java 8 it will just throw IllegalStateExceptions as you are trying to update the UI from a background thread. You need
if (event.getCode() == KeyCode.ENTER)
{
new Timer().schedule(
new TimerTask() {
int i;
#Override
public void run() {
String message = "hey"+i+"\n";
Platform.runLater(() -> textArea.appendText(message));
i++;
}
}, 0, 500);
}
I don't know if that will fix your performance issue or not. Appending text to a text area essentially involves doing lots of string concatenation; eventually (as the text in the text area gets long) this is going to be prohibitive. You might want to use a virtualized control (such as ListView), depending on the functionality you need.
I want to implement a GUI element that works like a button that reacts different on right/left clicks and dragging.
Say this button is an object. Who is in charge to call the right method on mouseevents from the button and how is it designed in usual programming languages like e.g. AWT in Java?
I don't claim that this is 'the professional way' but this is how I do it.
With lazy initialization and anonymous classes.
private JButton btnSpecialbutton;
private JButton getBtnSpecialbutton() {
if (btnSpecialbutton == null) {
btnSpecialbutton = new JButton("SpecialButton");
btnSpecialbutton.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
System.out.println("Dragged");
}
});
btnSpecialbutton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1)
{
System.out.println("Left Mouse Button");
}
else if(e.getButton() == MouseEvent.BUTTON3)
{
System.out.println("Right Mouse Button");
}
}
});
}
return btnSpecialbutton;
}
Are there any events that Visual Studio raises while debugging that I could subscribe to in the code? Specifically, I'd like to know when a debugger is detached or stops. Thanks
In a separate thread you could check the Debugger.IsAttached method and generate events accordingly.
EDIT: I just whipped this up, see if it works.
public class DebuggerEvents {
public delegate void DebuggingStatusChanged(EventArgs e);
public event DebuggingStatusChanged DebuggerAttached;
public event DebuggingStatusChanged DebuggerUnattached;
private readonly Thread workerThread;
public DebuggerEvents() {
var threadStart = new ThreadStart(BackgoundWorker);
workerThread = new Thread(threadStart);
workerThread.Start();
}
private void BackgoundWorker() {
bool currentStateflag = Debugger.IsAttached;
while (true) {
if (Debugger.IsAttached && !currentStateflag){
DebuggerAttached(new EventArgs());
currentStateflag = true;
}
if (!Debugger.IsAttached && currentStateflag){
DebuggerUnattached(new EventArgs());
currentStateflag = false;
}
Thread.Sleep(100);
}
}
}
Try this:
System.Diagnostics.Debugger.IsAttached
Also look up conditional compilation symbols and pay attention to the DEBUG symbol.
I am getting some errors thrown in my code when I open a Windows Forms form in Visual Studio's designer. I would like to branch in my code and perform a different initialization if the form is being opened by designer than if it is being run for real.
How can I determine at run-time if the code is being executed as part of designer opening the form?
if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
{
// Design time logic
}
To find out if you're in "design mode":
Windows Forms components (and controls) have a DesignMode property.
Windows Presentation Foundation controls should use the IsInDesignMode attached property.
The Control.DesignMode property is probably what you're looking for. It tells you if the control's parent is open in the designer.
In most cases it works great, but there are instances where it doesn't work as expected. First, it doesn't work in the controls constructor. Second, DesignMode is false for "grandchild" controls. For example, DesignMode on controls hosted in a UserControl will return false when the UserControl is hosted in a parent.
There is a pretty easy workaround. It goes something like this:
public bool HostedDesignMode
{
get
{
Control parent = Parent;
while (parent!=null)
{
if(parent.DesignMode) return true;
parent = parent.Parent;
}
return DesignMode;
}
}
I haven't tested that code, but it should work.
The most reliable approach is:
public bool isInDesignMode
{
get
{
System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
bool res = process.ProcessName == "devenv";
process.Dispose();
return res;
}
}
The most reliable way to do this is to ignore the DesignMode property and use your own flag that gets set on application startup.
Class:
public static class Foo
{
public static bool IsApplicationRunning { get; set; }
}
Program.cs:
[STAThread]
static void Main()
{
Foo.IsApplicationRunning = true;
// ... code goes here ...
}
Then just check the flag whever you need it.
if(Foo.IsApplicationRunning)
{
// Do runtime stuff
}
else
{
// Do design time stuff
}
I had the same problem in Visual Studio Express 2013. I tried many of the solutions suggested here but the one that worked for me was an answer to a different thread, which I will repeat here in case the link is ever broken:
protected static bool IsInDesigner
{
get { return (Assembly.GetEntryAssembly() == null); }
}
The devenv approach stopped working in VS2012 as the designer now has its own process. Here is the solution I am currently using (the 'devenv' part is left there for legacy, but without VS2010 I am not able to test that though).
private static readonly string[] _designerProcessNames = new[] { "xdesproc", "devenv" };
private static bool? _runningFromVisualStudioDesigner = null;
public static bool RunningFromVisualStudioDesigner
{
get
{
if (!_runningFromVisualStudioDesigner.HasValue)
{
using (System.Diagnostics.Process currentProcess = System.Diagnostics.Process.GetCurrentProcess())
{
_runningFromVisualStudioDesigner = _designerProcessNames.Contains(currentProcess.ProcessName.ToLower().Trim());
}
}
return _runningFromVisualStudioDesigner.Value;
}
}
/// <summary>
/// Are we in design mode?
/// </summary>
/// <returns>True if in design mode</returns>
private bool IsDesignMode() {
// Ugly hack, but it works in every version
return 0 == String.CompareOrdinal(
"devenv.exe", 0,
Application.ExecutablePath, Application.ExecutablePath.Length - 10, 10);
}
System.Diagnostics.Debugger.IsAttached
It's hack-ish, but if you're using VB.NET and when you're running from within Visual Studio My.Application.Deployment.CurrentDeployment will be Nothing, because you haven't deployed it yet. I'm not sure how to check the equivalent value in C#.
using (System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess())
{
bool inDesigner = process.ProcessName.ToLower().Trim() == "devenv";
return inDesigner;
}
I tried the above code (added a using statement) and this would fail on some occasions for me. Testing in the constructor of a usercontrol placed directly in a form with the designer loading at startup. But would work in other places.
What worked for me, in all locations is:
private bool isDesignMode()
{
bool bProcCheck = false;
using (System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess())
{
bProcCheck = process.ProcessName.ToLower().Trim() == "devenv";
}
bool bModeCheck = (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime);
return bProcCheck || DesignMode || bModeCheck;
}
Maybe a bit overkill, but it works, so is good enough for me.
The success in the example noted above is the bModeCheck, so probably the DesignMode is surplus.
You check the DesignMode property of your control:
if (!DesignMode)
{
//Do production runtime stuff
}
Note that this won't work in your constructor because the components haven't been initialized yet.
When running a project, its name is appended with ".vshost".
So, I use this:
public bool IsInDesignMode
{
get
{
Process p = Process.GetCurrentProcess();
bool result = false;
if (p.ProcessName.ToLower().Trim().IndexOf("vshost") != -1)
result = true;
p.Dispose();
return result;
}
}
It works for me.
I'm not sure if running in debug mode counts as real, but an easy way is to include an if statement in your code that checkes for System.Diagnostics.Debugger.IsAttached.
If you created a property that you don't need at all at design time, you can use the DesignerSerializationVisibility attribute and set it to Hidden. For example:
protected virtual DataGridView GetGrid()
{
throw new NotImplementedException("frmBase.GetGrid()");
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int ColumnCount { get { return GetGrid().Columns.Count; } set { /*Some code*/ } }
It stopped my Visual Studio crashing every time I made a change to the form with NotImplementedException() and tried to save. Instead, Visual Studio knows that I don't want to serialize this property, so it can skip it. It only displays some weird string in the properties box of the form, but it seems to be safe to ignore.
Please note that this change does not take effect until you rebuild.
We use the following code in UserControls and it does the work. Using only DesignMode will not work in your app that uses your custom user controls as pointed out by other members.
public bool IsDesignerHosted
{
get { return IsControlDesignerHosted(this); }
}
public bool IsControlDesignerHosted(System.Windows.Forms.Control ctrl)
{
if (ctrl != null)
{
if (ctrl.Site != null)
{
if (ctrl.Site.DesignMode == true)
return true;
else
{
if (IsControlDesignerHosted(ctrl.Parent))
return true;
else
return false;
}
}
else
{
if (IsControlDesignerHosted(ctrl.Parent))
return true;
else
return false;
}
}
else
return false;
}
Basically the logic above boils down to:
public bool IsControlDesignerHosted(System.Windows.Forms.Control ctrl)
{
if (ctrl == null) return false;
if (ctrl.Site != null && ctrl.Site.DesignMode) return true;
return IsControlDesignerHosted(ctrl.Parent);
}
If you are in a form or control you can use the DesignMode property:
if (DesignMode)
{
DesignMode Only stuff
}
I found the DesignMode property to be buggy, at least in previous versions of Visual Studio. Hence, I made my own using the following logic:
Process.GetCurrentProcess().ProcessName.ToLower().Trim() == "devenv";
Kind of a hack, I know, but it works well.
System.ComponentModel.Component.DesignMode == true
To solve the problem, you can also code as below:
private bool IsUnderDevelopment
{
get
{
System.Diagnostics.Process process = System.Diagnostics.Process.GetCurrentProcess();
if (process.ProcessName.EndsWith(".vshost")) return true;
else return false;
}
}
Here's another one:
//Caters only to thing done while only in design mode
if (App.Current.MainWindow == null){ // in design mode }
//Avoids design mode problems
if (App.Current.MainWindow != null) { //applicaiton is running }
After testing most of the answers here, unfortunately nothing worked for me (VS2015).
So I added a little twist to JohnV's answer, which didn't work out of the box, since DesignMode is a protected Property in the Control class.
First I made an extension method which returns the DesignMode's Property value via Reflection:
public static Boolean GetDesignMode(this Control control)
{
BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static;
PropertyInfo prop = control.GetType().GetProperty("DesignMode", bindFlags);
return (Boolean)prop.GetValue(control, null);
}
and then I made a function like JohnV:
public bool HostedDesignMode
{
get
{
Control parent = Parent;
while (parent != null)
{
if (parent.GetDesignMode()) return true;
parent = parent.Parent;
}
return DesignMode;
}
}
This is the only method that worked for me, avoiding all the ProcessName mess, and while reflection should not be used lightly, in this case it did all the difference! ;)
EDIT:
You can also make the second function an extension method like this:
public static Boolean IsInDesignMode(this Control control)
{
Control parent = control.Parent;
while (parent != null)
{
if (parent.GetDesignMode())
{
return true;
}
parent = parent.Parent;
}
return control.GetDesignMode();
}
For WPF (hopefully this is useful for those WPF people stumbling upon this question):
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject()))
{
}
GetIsInDesignMode requires a DependencyObject. If you don't have one, just create one.
/// <summary>
/// Whether or not we are being run from the Visual Studio IDE
/// </summary>
public bool InIDE
{
get
{
return Process.GetCurrentProcess().ProcessName.ToLower().Trim().EndsWith("vshost");
}
}
Here's a flexible way that is adaptable to where you compile from as well as whether or not you care which mode you're in.
string testString1 = "\\bin\\";
//string testString = "\\bin\\Debug\\";
//string testString = "\\bin\\Release\\";
if (AppDomain.CurrentDomain.BaseDirectory.Contains(testString))
{
//Your code here
}